How to Fix VBA Run-time Error 91 When Excluding Captions in Word
Question details
The user needs to fix a run-time error 91 in a Word VBA macro designed to exclude specific elements from the document word count.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA script to calculate the total word count of a document while intentionally ignoring elements like tables and captions.
- Observed behavior
- The macro generates run-time error 91 when it attempts to check if a paragraph uses the Caption style via the objParagraph.Range.Style property.
Ensure you have saved a backup of your current document and press ALT + F11 to open the Visual Basic for Applications (VBA) editor where your macro is stored.
Modify the VBA Code to Check Paragraph Style Directly
Update the script to directly query the style property of the paragraph object instead of querying the paragraph's range.
Run-time error 91 usually occurs when an object variable is not properly set. In this specific scenario, attempting to access the style property through the paragraph's range object can fail and trigger the error. By querying the style property directly on the paragraph object itself, the macro can securely identify the style without causing reference errors.
Press the ALT + F11 keys on your keyboard to launch the Visual Basic editor within your word processor.
In the Project Explorer pane on the left, double-click the module containing your word count macro to open the code.
Scroll through the code to locate the line that checks for the caption style, which is currently written as: If objParagraph.Range.Style = "Caption" Then
Delete the ".Range" portion of the command so that the code checks the style directly. Replace the line with: If objParagraph.Style = "Caption" Then
Click the Save icon, close the VBA editor, and run your macro again to successfully calculate the word count without captions.

Run Custom Document Macros Smoothly in WPS Writer
WPS Office provides robust built-in support for VBA macros in its advanced versions, allowing you to seamlessly run and edit your custom word count scripts. It offers a lightweight environment that perfectly balances advanced document formatting with fast performance.
- 1. Enable Developer Tools: Open WPS Writer, navigate to the Developer tab, and ensure your macro security settings allow code execution.
- 2. Access the VBA Editor: Click the Visual Basic icon or press ALT + F11 to open the integrated macro editor.
- 3. Insert Your Script: Paste your corrected word count macro into a new module, ensuring you use the direct paragraph style check.
- 4. Execute the Code: Run the script directly from the developer tab to instantly calculate your adjusted word count.

Frequently Asked Questions
What causes Run-time error 91 in VBA?
Run-time error 91 ('Object variable or With block variable not set') occurs when a script attempts to use an object variable that has not been initialized properly or evaluates to nothing, such as referencing a range property that cannot be correctly mapped.
Why does objParagraph.Style work while objParagraph.Range.Style fails?
Accessing the style through the .Range object can occasionally fail if the range contains mixed formatting or cannot be precisely resolved by the application. Querying the .Style property directly on the paragraph object relies on a more stable reference.
Can I exclude other elements like tables using a similar VBA method?
Yes. Within your loop, you can add conditional checks for other document elements. For example, you can check if a range belongs to a table by verifying if objParagraph.Range.Information(wdWithInTable) is True, and skip adding those words to the total count.
Does WPS Office support VBA macro scripts?
Yes, WPS Office supports VBA macros in its professional and premium versions. Most custom macros written for Microsoft Word documents will run smoothly in WPS Writer, provided they do not rely on heavily restricted external libraries.




