Create a Word Keyword Frequency and Location Report with VBA
Question details
The user wants to generate a comprehensive report that counts how many times specific keywords appear in a document and lists their exact page and line locations using a VBA macro.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Analyzing a long document for specific terms or multi-word phrases and generating an organized summary table of their occurrences.
- Observed behavior
- Requires a programmatic VBA solution to reliably find whole words, track their location references, sort the results, and automatically build an output table.
Ensure that the Developer tab is enabled in your word processor and that your security settings allow macros to run.
Write a VBA Macro to Search, Count, and Log Keyword Locations
Create a custom VBA script that loops through a defined list of target words, searches the document, captures location data using built-in properties, and generates a results table.
To achieve this, the macro must be configured to search for each term individually, ensuring whole-word matches are enforced so partial words are not falsely counted.
You can define your target terms using an array or a pipe-delimited string, and utilize a Dictionary object to keep track of word frequencies and ignore lists.
Press Alt + F11 to launch the Visual Basic for Applications (VBA) editor, then insert a new Module from the Insert menu.
Set up your target terms either in a pipe-delimited list string (e.g., 'term1|term2|phrase 3') and use the Split function to create an array, or pull them from a separate text file.
Use the Find.Execute method in a loop. Ensure you set MatchWholeWord = True so the script only counts exact word matches and handles multi-word phrases correctly.
Inside the successful Find loop, use the Selection.Information(wdActiveEndPageNumber) and Selection.Information(wdFirstCharacterLineNumber) properties to record where the keyword was found.
Have the macro output the collected data array into a new blank document, insert a table based on the data ranges, and apply a Sort method to organize the results alphabetically or by frequency.

Generate Keyword Reports with Macros in WPS Writer
WPS Office supports VBA macros in its advanced editions, allowing you to seamlessly run your keyword frequency and location scripts. It offers a familiar interface for managing your document automation tasks efficiently.
- 1. Open your document: Launch WPS Writer and open the document you wish to analyze.
- 2. Access the VBA Editor: Go to the Developer tab and click on 'Macros' or 'Visual Basic' to open the coding environment.
- 3. Paste and run the code: Insert a new module, paste your keyword frequency VBA code, and click Run to instantly generate your sorted results table.

Frequently Asked Questions
How do I ensure my macro only counts whole words?
In your VBA code, set the MatchWholeWord property of the Find object to True. This prevents the macro from counting partial matches, such as counting 'the' when it appears inside the word 'there'.
Can I search for multi-word phrases using VBA?
Yes, you can define multi-word phrases in your target list just like single words. The Find.Execute method inherently supports spaces and will locate the exact phrase within the document text.
How do I capture the exact page and line number in Word VBA?
You can use the Selection.Information property. Specifically, wdActiveEndPageNumber retrieves the page number, and wdFirstCharacterLineNumber retrieves the line number of the currently selected or found text.




