How to Find Multiple Words in a Word Document with VBA
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.

- 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.
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.
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.
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.
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.
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.

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. Download and Install: Get WPS Office from the official website and complete the straightforward installation process.
- 2. Open Your Macro Document: Launch WPS Writer and open your .docm or .doc file containing the multi-word search VBA script.
- 3. Enable Developer Tools: Navigate to the Developer tab on the top ribbon to access the Macro editor and run your scripts flawlessly.

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.




