logo
search
VBA & Macro Problems

How to Find Multiple Words in a Word Document with VBA

WPS EditorWPS Editor Sep 27, 2026 869 views

Question details

The user wants to create a Word VBA macro to find multiple keywords by comparing them with another document using a file picker, handling both case-insensitive and prefix matches.

How to Find Multiple Words in a Word Document Using VBA
Product
Microsoft Word / WPS Writer
Device & OS
not provided
Scenario
Comparing a list of keywords against another target document to find exact or prefix matches using automated VBA scripts.
Observed behavior
Needs to implement a file picker instead of a folder picker, ignore text case during comparison, and accurately identify keyword prefixes within the target document's text.
Before you start

Enable the Developer tab in your word processor to access the VBA editor, and ensure you have both the source keyword document and the target document saved locally on your computer.

Solution 1Recommended

Implement the File Picker and Case-Insensitive Search

Use the correct file dialogue object and text conversion functions to search for keywords regardless of their capitalization.

By default, Word VBA's InStr function is case-sensitive, which might cause it to miss capitalized variations of a keyword. Additionally, using a folder picker instead of a file picker prevents you from targeting a specific Word document.

1
Open the File Picker

In your VBA editor, locate your FileDialog code. Replace the folder picker code with 'Set FD = Application.FileDialog(msoFileDialogFilePicker)' to allow the macro to select a specific Word document.

2
Implement Case-Insensitive Matching

Use the 'InStr' and 'LCase' functions to ignore capitalization. Add the condition 'If InStr(LCase(DocTarget.Range.Text), LCase(strKeyword)) > 0 Then' to your matching loop.

3
Add Prefix Matching

To check if a keyword matches the beginning of the text, use 'If Left(LCase(DocTarget.Range.Text), Len(strKeyword)) = LCase(strKeyword) Then'. This compares the string precisely at the start position.

Implement the File Picker and Case-Insensitive Search
Search Precision: You can further refine your search accuracy by adding 'Replace' functions to strip out punctuation or trailing spaces in the text before running the comparison.
Advanced Macro Support in WPS

Use WPS Office for Seamless Macro and VBA Processing

WPS Office provides excellent compatibility with Word VBA macros, allowing you to run complex scripts like multi-word searches directly within WPS Writer. It is lightweight, user-friendly, and perfect for your document automation needs.

  1. 1. Download and Install: Get WPS Office from the official website and complete the straightforward installation process.
  2. 2. Open Your Macro Document: Launch WPS Writer and open your .docm or .doc file containing the multi-word search VBA script.
  3. 3. Enable Developer Tools: Navigate to the Developer tab on the top ribbon to access the Macro editor and run your scripts flawlessly.
Fully compatible with Microsoft Word macro-enabled documents (.docm, .doc)Supports executing and editing VBA macros seamlesslyLightweight application with a familiar, highly productive tabbed interfaceFree and easy to download for rapid deployment
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA macro selecting a folder instead of a document?

You are likely using 'msoFileDialogFolderPicker' in your code. Change this specific parameter to 'Application.FileDialog(msoFileDialogFilePicker)' to open a file selection dialog instead.

How can I ignore punctuation when searching for words in VBA?

You need to use the 'Replace' function to substitute punctuation marks with empty strings (e.g., Replace(text, ",", "")) in both your target text and keywords before running the InStr comparison.

Is the InStr function in VBA case-sensitive by default?

Yes, InStr performs a binary (case-sensitive) comparison by default. You can bypass this by wrapping both compared strings in the LCase() function, or by specifying 'vbTextCompare' as the final argument in the InStr function.