logo
search
VBA & Macro Problems

How to Export Selected Word Pages to PDF Using a VBA Macro

Maira MehtabMaira Mehtab Sep 22, 2026 868 views

Question details

The user needs a Word VBA macro to automatically export specific pages to a PDF, display a Save As dialog with a dynamic file name, and open the PDF automatically after saving.

Product
Microsoft Word
Device & OS
not provided
Scenario
Automating the export of a specific range of pages (e.g., page 2 to 100) from a Word document to a PDF file, while using document data like Quick Parts to populate the suggested file name.
Observed behavior
The user requires a VBA solution that removes manual prompts to always default to a specific page range, dynamically names the file using Quick Parts or Content Controls, and automatically opens the PDF upon creation.
Before you start

Ensure that the Developer tab is enabled in your Word ribbon to access the VBA editor, and verify that macros are enabled in your Trust Center settings.

Solution 1Recommended

Create a VBA Macro to Export Pages as PDF

Use this VBA code to export a specific page range, assign a dynamic file name using document fields, and auto-open the PDF after saving.

By utilizing Word's ExportAsFixedFormat method, you can specify exact page ranges to export without manual prompts. Combining this with the FileDialog object allows you to display a Save As prompt with a pre-filled name generated from content controls or Quick Parts.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor. Go to Insert > Module to create a new module for your macro.

2
Extract Text for the File Name

Declare variables to read the content controls or Quick Parts in your document. Construct your desired file name string (e.g., 'Status Consignment Agreement - Account Name - Date').

3
Configure the FileDialog Object

Set up a msoFileDialogSaveAs dialog. Assign your dynamically constructed file name string to the .InitialFileName property before calling .Show to present the Save As dialog.

4
Execute the PDF Export

If the user confirms the Save As dialog, call the ExportAsFixedFormat method using the saved path. Use the parameters Range:=wdExportFromTo, From:=2, To:=100, and OpenAfterExport:=True to generate and open the file.

Customizing the Page Range: You can adjust the 'From' and 'To' values in the ExportAsFixedFormat method if your document structure changes or if you need to extract a different set of pages.
Built-in PDF Export

Export Specific Pages to PDF Easily with WPS Office

WPS Office provides robust VBA support along with highly intuitive native PDF conversion tools. You can easily execute macros or use built-in features to export specific page ranges to PDF without complex coding.

  1. 1. Open Your Document: Launch WPS Writer and open the document you wish to convert.
  2. 2. Access the PDF Export Tool: Click on the 'Menu' button in the top left corner and select 'Export to PDF'.
  3. 3. Set the Custom Page Range: In the export dialog box, choose the 'Custom Range' option and input your desired page numbers (e.g., 2-100).
  4. 4. Export and Open: Ensure the 'Open after export' checkbox is ticked, then click 'Export' to save and instantly view your PDF.
Native feature to export specific page ranges to PDF directlyHighly compatible with Microsoft Office Word (.docx) formatsSupports executing traditional VBA macros seamlesslyLightweight, fast execution, and user-friendly interface
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA macro exporting all pages instead of the selected range?

Ensure you are using the Range:=wdExportFromTo parameter in the ExportAsFixedFormat method. If you mistakenly use Range:=wdExportAllDocument, it will ignore your From and To page parameters entirely.

Can I make the exported PDF open automatically after saving in VBA?

Yes, you can achieve this by setting the OpenAfterExport parameter to True within the ExportAsFixedFormat method. This instructs the application to launch your system's default PDF viewer immediately after the file is generated.

How do I extract text from Content Controls to use in the PDF filename?

You can access Content Controls via VBA by referencing ActiveDocument.SelectContentControlsByTitle("YourTitle").Item(1).Range.Text. Simply assign this text to a string variable and append it to your InitialFileName property before displaying the FileDialog.

How do I include Quick Parts in my VBA generated filename?

If your Quick Parts represent mapped document properties, retrieve them using ActiveDocument.BuiltInDocumentProperties("PropertyName").Value. Concatenate these values into your main filename string to dynamically construct titles like 'Agreement - Account Name - Date'.