Fix Word VBA Error 91 Object Variable When Accessing Specific ContentControl
🔍 WiseChecker

Fix Word VBA Error 91 Object Variable When Accessing Specific ContentControl

You see Word VBA Error 91: Object Variable or With Block Variable Not Set when your macro tries to access a specific ContentControl. This error occurs because the code references a ContentControl object that does not exist, has not been assigned, or has been released from memory. The macro attempts to use an object variable that holds a value of Nothing, so Word cannot perform any method or property on it. This article explains the exact causes of Error 91 with ContentControls and provides tested steps to identify, assign, and safely reference the target ContentControl in your VBA project.

Key Takeaways: Fixing VBA Error 91 for ContentControl Objects

  • Set the object variable with the Set keyword: Without Set, the variable stays Nothing and causes Error 91.
  • Verify the ContentControl exists before accessing it: Check the count of ActiveDocument.ContentControls or use a specific index or title.
  • Use early binding and explicit WithEvents: Declare the object as ContentControl type to enable IntelliSense and avoid untyped variables.

ADVERTISEMENT

Why Word VBA Error 91 Occurs When Accessing a ContentControl

Error 91 means your code is trying to use an object variable that has not been set to a valid object. In VBA, an object variable must be assigned using the Set keyword before you can read its properties or call its methods. If you declare a variable as Dim cc As ContentControl but then write cc = ActiveDocument.ContentControls(1) without the Set keyword, the variable remains Nothing. Any subsequent line such as cc.Range.Text will raise Error 91.

Another common cause is referencing a ContentControl that does not exist in the document. For example, using ContentControls(99) when the document contains only five controls, or using ContentControls("NameThatDoesNotExist"). The index or title must match an existing control exactly. A third cause is that the ContentControl object has been deleted or the document collection has been modified between the time you assigned the variable and the time you use it. This can happen when a macro loops through controls and removes one, leaving the reference invalid.

Steps to Diagnose and Fix the Object Variable Error

Follow these steps to correct the VBA code and eliminate Error 91 when working with ContentControls. The steps assume you have the Visual Basic Editor open with the macro that produces the error.

  1. Add the Set keyword to the object assignment
    Locate the line where you assign the ContentControl to a variable. Change cc = ActiveDocument.ContentControls(1) to Set cc = ActiveDocument.ContentControls(1). The Set keyword is mandatory for all object assignments in VBA. Without it, the variable remains Nothing.
  2. Verify the ContentControl index or title exists
    Before accessing a specific ContentControl, check that it exists. Use the Count property: If ActiveDocument.ContentControls.Count >= 1 Then Set cc = ActiveDocument.ContentControls(1). For title-based access, use a loop: For Each cc In ActiveDocument.ContentControls: If cc.Title = "MyTitle" Then Exit For: Next. This avoids hard-coding an index that may change.
  3. Declare the variable with the correct object type
    Use Dim cc As ContentControl instead of Dim cc As Object or Dim cc. Early binding gives you IntelliSense and catches type mismatches at compile time. If the reference to the Word object library is missing, go to Tools > References and check "Microsoft Word 16.0 Object Library."
  4. Ensure the document is the active document
    If your macro runs on a document that is not the active window, use Documents("YourDocName.docx") instead of ActiveDocument. Assign the document object to a variable first: Dim doc As Document: Set doc = Documents("YourDocName.docx"). Then use doc.ContentControls.
  5. Test the macro step by step with the Locals window
    Press F8 to step through the code one line at a time. Open the View > Locals Window. Watch the value of your ContentControl variable. If it shows "Nothing" when you reach a line that accesses it, you have an assignment or existence problem. Correct the line that should set the variable.
  6. Use error handling to catch the failure gracefully
    Add an On Error Resume Next before the problematic line, then check if the variable is Nothing afterward. Example: On Error Resume Next: Set cc = ActiveDocument.ContentControls(99): If cc Is Nothing Then MsgBox "Control not found.": Exit Sub: On Error GoTo 0. This prevents the macro from crashing and tells you exactly which control is missing.

ADVERTISEMENT

If Word Still Has Issues After the Main Fix

Error 91 appears after deleting a ContentControl in the same macro

When you delete a ContentControl from the document using cc.Delete, the object variable becomes invalid. Any subsequent reference to that variable triggers Error 91. The fix is to set the variable to Nothing after deletion and avoid using the same variable again. Example: cc.Delete: Set cc = Nothing. If you need to work with another control, assign a new object to the variable using Set.

Error 91 when looping through ContentControls by index

If you delete controls inside a For loop that uses an index number, the collection shrinks and the index pointer becomes incorrect. For example, deleting control 3 shifts control 4 to position 3. The loop then skips the new control 3. This can cause an index-out-of-range error or Error 91 if the loop continues past the last control. The solution is to loop backwards: For i = ActiveDocument.ContentControls.Count To 1 Step -1. This way, deletions do not affect the indexes of remaining controls.

Error 91 when the document is protected

If the document has editing restrictions, your macro may not be able to access the ContentControls collection. Check if ActiveDocument.ProtectionType is not wdAllowOnlyFormFields or wdAllowOnlyComments. To access ContentControls in a protected document, you must unprotect it first: If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect. After making changes, you can reapply protection with the same password.

VBA Coding Patterns That Avoid Error 91 With ContentControls

Pattern Correct Code Incorrect Code
Variable assignment Set cc = ActiveDocument.ContentControls(1) cc = ActiveDocument.ContentControls(1)
Existence check If ActiveDocument.ContentControls.Count > 0 Then No check, assume control exists
Loop with deletion For i = Count To 1 Step -1 For i = 1 To Count
Document object Set doc = ActiveDocument: Set cc = doc.ContentControls(1) Set cc = ContentControls(1) without document reference

Each pattern in the table addresses a specific cause of Error 91. Use the correct code column as a template for your own macros.

You can now identify the exact line causing Error 91 in your VBA project and correct the object assignment, existence check, or document reference. Next, apply the Set keyword to every object variable assignment and add a Count check before accessing a ContentControl by index. For advanced protection, write a reusable function that returns a valid ContentControl or Nothing, and call it from any macro that needs to access a specific control.

ADVERTISEMENT