Word Macro to Export All Matching Pages to a Single PDF
Question details
The user needs a Word macro that searches for a specific keyword and saves all pages containing that keyword into one combined PDF file, rather than generating a separate PDF for each match.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document extraction by finding pages with a specific target word and exporting only those relevant pages as a single file.
- Observed behavior
- The current macro executes the print or export command inside the search loop, resulting in a separate one-page PDF file for every single keyword match.
Ensure your document is saved and that you have enabled Developer tools and macros in your security settings before attempting to run VBA code.
Collect Matching Page Numbers and Export as a Single PDF
Instead of triggering an export inside the search loop, collect the page numbers into a string variable first, and print that specific range once at the end of the macro.
When you call the PrintOut or ExportAsFixedFormat method inside a Find loop, Word triggers a separate print or export job for each individual match. To consolidate the output into a single PDF, you must first build a comma-separated list of the matching page numbers. Once the loop finishes, you use that string to export the collected pages in one operation.
Press `Alt + F11` on your keyboard to open the Visual Basic for Applications (VBA) Editor.
Navigate to `Insert` > `Module` from the top menu to create a blank workspace for your new macro.
Begin your macro with a valid declaration, such as `Sub ExportMatchesToPDF()`, and declare variables to hold the page numbers (e.g., `Dim strPages As String`). End the macro with `End Sub`.
Use the `.Find` method to search for your target word. Inside the loop, retrieve the current page number using `Selection.Information(wdActiveEndPageNumber)`. Add logic to append this number to `strPages` separated by a comma, ensuring you avoid adding duplicate page numbers if the word appears multiple times on the same page.
After the `Loop` concludes, add the command `ActiveDocument.ExportAsFixedFormat` or `ActiveDocument.PrintOut`. Set the `Range` parameter to `wdPrintRangeOfPages` and pass your `strPages` variable to the `Pages` parameter to generate a single PDF containing all matched pages.

Automate Document Tasks with WPS Office
WPS Office provides robust VBA and macro support, allowing you to seamlessly run customized scripts to search documents, manipulate data, and export specific pages to PDF natively without installing additional plugins.
- 1. Open your Document in WPS Writer: Launch WPS Office and open the document you wish to search.
- 2. Access the Developer Tab: Navigate to the Developer tab on the ribbon and click on 'Macros' to open the VBA editor.
- 3. Paste and Run Your Macro: Insert your VBA code for collecting page numbers and exporting to PDF, then press F5 to run the script and generate your single PDF file.

Frequently Asked Questions
Why does my current macro create multiple PDFs instead of one?
If your macro contains the `PrintOut` or `ExportAsFixedFormat` command inside the `.Find` loop, it executes a new print/export job for every match it finds. You must move the print command outside the loop and use a string variable to collect the page numbers first.
How do I prevent printing the same page twice if the word appears multiple times on it?
You can add a condition inside your VBA loop to check if the current page number is already present in your `strPages` variable. If it is already listed, the macro should skip appending it again.
How do I enable the Developer tab to use macros?
Go to File > Options > Customize Ribbon, and check the box next to 'Developer'. This will display the Developer tab on your main interface, giving you access to the VBA Editor and Macro settings.
Can I search for multiple different words and export those pages?
Yes, you can modify the VBA logic to perform sequential searches for multiple keywords. Simply append all unique matching page numbers from each search into your main page number string before executing the final PDF export command.




