VBA & Macro Problems

Create a Word Keyword Frequency and Location Report with VBA

Emma BrownEmma Brown Oct 28, 2026 870 views

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.

How to Create a Word Keyword Frequency and Location Report with VBA
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.
Before you start

Ensure that the Developer tab is enabled in your word processor and that your security settings allow macros to run.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to launch the Visual Basic for Applications (VBA) editor, then insert a new Module from the Insert menu.

2
Define the target keywords

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.

3
Configure the Find loop

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.

4
Capture page and line locations

Inside the successful Find loop, use the Selection.Information(wdActiveEndPageNumber) and Selection.Information(wdFirstCharacterLineNumber) properties to record where the keyword was found.

5
Generate the sorted table

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.

Write a VBA Macro to Search, Count, and Log Keyword Locations
Advanced Filtering: If you are using a Dictionary object to filter out common words, you can replace standard ignore-list entries with your specific words to process. Simply change your condition to 'If Ignore.Exists(Key) = False Then GoTo Skip' to process only your desired keywords.

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. 1. Open your document: Launch WPS Writer and open the document you wish to analyze.
  2. 2. Access the VBA Editor: Go to the Developer tab and click on 'Macros' or 'Visual Basic' to open the coding environment.
  3. 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.
High compatibility with Microsoft Word VBA scripts and macro formats (.docm).Lightweight architecture for fast document processing and text searching.Easily generate output tables and analytical reports with built-in tools.
microsoft office alternative - wps office

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.