PDF Conversion Issues

How to Create a PDF from Word Excluding Pages with Specific Text

Ayan MasoodAyan Masood Oct 28, 2026 869 views

Question details

The user wants to generate a PDF from a Word document by dynamically filtering out and excluding pages that contain a specific keyword.

How to Create a PDF from Word Excluding Pages with Specific Text
Product
Microsoft Word
Device & OS
not provided
Scenario
Exporting a tailored PDF from a larger Word document by hiding pages containing specific sensitive or irrelevant text, such as the word PROTOCOL.
Observed behavior
Automating the process of finding a specific word, hiding its respective pages, updating the document fields, exporting the visible pages to PDF, and restoring the original document structure.
Before you start

Test the macro on a backup copy of your document, because temporarily hiding page content can affect fields, tables of contents, cross-references, and page numbering dynamically.

Solution 1Recommended

Use a VBA Macro to Conditionally Hide Pages and Export to PDF

This automated approach searches for a specified keyword, temporarily sets the font of those pages to hidden, prints or exports the document to PDF, and then reverts the changes using UndoRecord.

Running this VBA script will prompt you for a keyword. It identifies each matching page, hides the content, updates fields to reflect the remaining pages, generates a PDF in the same folder as the source document, and undoes the hidden text formatting immediately after.

1
Open the VBA Editor

Open your Word document, press Alt + F11 to launch the Microsoft Visual Basic for Applications editor, and click Insert > Module from the top menu.

2
Paste the Macro Script

Copy and paste the ConditionalPrint macro into the module window. Ensure the code includes the Application.UndoRecord and Options.PrintHiddenText commands to safely reverse the changes after exporting.

3
Run the Macro

Press F5 or click the Run button. A prompt box will appear asking: 'What text should be found on pages to be suppressed?'

4
Enter the Target Text

Type the specific word you want to exclude (e.g., PROTOCOL) and click OK. The macro will process the document, save an equivalent PDF file in the original document's folder, and restore the Word file to its original state.

Impact on Page Numbering: Decide whether the exported PDF should preserve original page numbers or dynamically renumber the remaining pages. Word will automatically recalculate visible page numbers when content is hidden unless the page numbers are unlinked or hardcoded as static text beforehand.
Free Microsoft Office alternative

Edit and Manage Word and PDF Documents with WPS Office

Avoid complex VBA scripts and macro troubleshooting. WPS Office provides a lightweight, highly compatible alternative to Microsoft Office, featuring built-in PDF conversion and advanced page extraction tools so you can visually manage your PDFs.

  1. 1. Export to PDF: Open your original document in WPS Writer and click Menu > Export to PDF.
  2. 2. Open PDF in WPS: Open the newly generated PDF document using the built-in WPS PDF viewer.
  3. 3. Search for Specific Text: Press Ctrl + F to search for the specific text you wish to remove and note the corresponding page numbers.
  4. 4. Delete or Extract Pages: Navigate to the Page tab on the top ribbon, select the identified pages, and click Delete to create a clean version of your document.
Free and lightweight alternative to Microsoft Office.Fully compatible with Microsoft Word (.doc, .docx) and macro-enabled formats.Built-in PDF editor to visually extract, delete, or merge specific pages without coding.Familiar tabbed user interface for a seamless transition.
microsoft office alternative - wps office

Frequently Asked Questions

Will hiding pages affect my Table of Contents in the exported PDF?

Yes, temporarily hiding pages via macros or manual formatting will alter the Table of Contents, cross-references, and page numbering because the content is mathematically removed from the layout. The macro updates fields before exporting to ensure the Table of Contents matches the remaining visible pages.

Where does the VBA macro save the generated PDF file?

The provided macro automatically saves the generated PDF file in the exact same directory folder as the original Word document. It uses the same file name but changes the extension to .pdf.

Can I use this macro if I want to keep the original page numbering intact?

If you want to maintain the original continuous page numbering despite excluding certain pages, you must unlink your headers/footers or convert dynamic page number fields into plain text before running the macro. Otherwise, Word will automatically recalculate the numbers for the visible pages.