VBA & Macro Problems

How to Find Words Inside or Outside Quotation Marks Using Word VBA Macros

Huma Ashraf ChHuma Ashraf Ch Nov 4, 2026 870 views

Question details

The user needs a Word macro to search for a specific list of words and selectively highlight them depending on whether they are located inside or outside of quotation marks.

How to Find Words Inside or Outside Quotation Marks Using Word VBA Macros
Product
Word
Device & OS
not provided
Scenario
Document editing and proofreading where specific target terms must be reviewed conditionally based on their inclusion within quotes.
Observed behavior
The user wants to process an array of multiple target words (e.g., knew, heard, saw) in a single macro execution rather than maintaining separate macros for each individual word.
Before you start

Before running any macro, verify whether your document uses straight quotes or smart (curly) quotes, as your VBA code will need to account for the specific character codes of the quotation marks you are targeting.

Solution 1Recommended

Create a VBA Macro to Conditionally Search Around Quotation Marks

Use a custom VBA script that loops through an array of target words and checks the surrounding characters to determine if they are enclosed in quotation marks.

To process multiple terms efficiently, store the target words in a VBA array. The macro will iterate through the document range for each word. When it finds a match, it expands the range to check the enclosing characters.

Based on the defined condition (inside or outside quotes), the macro will then apply highlighting or text replacement. Working with a copy of the document is recommended while testing.

1
Launch the VBA Editor

Open your document, navigate to the Developer tab on the ribbon, and click 'Visual Basic' (or press Alt + F11) to open the VBA Editor.

2
Insert a New Module

In the VBA Editor, right-click on your document project in the left pane, select 'Insert', and then click 'Module'.

3
Define the Word Array

Declare an array variable in your script to hold all target words. For example: arrWords = Array("knew", "heard", "saw").

4
Write the Search and Check Logic

Implement a 'For Each' loop to iterate through arrWords. Use the Find object to locate the word, then use the Range.MoveStartUntil and Range.MoveEndUntil methods to detect surrounding quotation characters (Chr(34), Chr(147), or Chr(148)).

5
Execute the Macro

Close the VBA Editor, return to the Developer tab, click 'Macros', select your newly created script, and click 'Run' to highlight the matching words.

Create a VBA Macro to Conditionally Search Around Quotation Marks
Account for Smart Quotes: Always include logic for both straight quotes (Chr(34)) and smart quotes (Chr(147) for left and Chr(148) for right) to ensure accurate detection across dynamically formatted documents.

Use WPS Writer to Run VBA Macros for Advanced Editing

WPS Office Writer fully supports VBA macros, allowing editors and writers to automate complex formatting tasks seamlessly, including searching and highlighting conditionally based on quotation marks.

  1. 1. Open your document: Launch WPS Writer and open the document you wish to edit.
  2. 2. Access the Developer Tab: Navigate to the 'Tools' tab and click on 'Developer' to reveal the macro toolset.
  3. 3. Launch the Visual Basic Editor: Click 'Visual Basic Editor' to paste or write your custom quotation mark search macro.
  4. 4. Run your Macro: Save your document as a macro-enabled format (.docm), click 'Macros', select your script, and hit 'Run'.
Full compatibility with Microsoft Word documents (.docx) and macro-enabled files (.docm).Built-in robust VBA editor to write, edit, and troubleshoot custom editing macros.Lightweight architecture processes heavy iterative macros quickly without lagging.Familiar ribbon interface makes accessing the Developer tab and macro tools highly intuitive.
QA img-9

Frequently Asked Questions

Why does my macro miss words enclosed in curly smart quotes?

Word processors automatically convert straight quotes to curly smart quotes by default. If your VBA code only searches for straight quotes (character code 34), it will skip smart quotes. You must explicitly include character codes for smart quotes (Chr(147) and Chr(148)) in your macro's search conditions.

How can I process multiple target words without creating separate macros?

You can declare an array variable in your VBA code (e.g., myWords = Array("knew", "heard", "saw")). Wrap your search and highlight logic inside a 'For Each' loop that iterates through the array, running the commands for every word in a single execution.

Where can I find pre-written editing macros for tasks like this?

There are dedicated online resources and communities for editing macros. A highly recommended free resource is the ebook 'Macros for Editors' by Paul Beverley, which contains over 800 pre-written VBA macros tailored for complex writing and editing tasks that can be easily adapted to your specific rules.