Export Excel Worksheet to PDF Preserving Hyperlinks using VBA
Question details
The user wants to automate the process of exporting a specific worksheet to a PDF file using VBA, while ensuring that all hyperlinks within the sheet remain active and clickable in the resulting PDF.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating document generation and reporting where active hyperlinks must be retained in the final PDF output without manual conversion.
- Observed behavior
- The goal state is to execute a macro that saves the specified worksheet as a PDF in the same directory as the workbook, keeping all hyperlinks fully functional.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and verify that the Developer tab is enabled on your ribbon to access the VBA Editor.
Use ExportAsFixedFormat in a VBA Module
Utilize the native ExportAsFixedFormat method in VBA to automate PDF creation while embedding document properties that keep hyperlinks active.
The ExportAsFixedFormat method is the most reliable way to convert worksheets to PDF via VBA while retaining document properties and hyperlinks.
The provided macro will automatically generate the PDF in the exact same directory as your original workbook and open it upon completion.
Open your workbook and press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.
In the VBA Editor, click on 'Insert' in the top menu bar, and select 'Module' to create a new blank standard module.
Paste the following script into the module window: Sub SaveAsPDFWithHyperlinks() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Main Sheet") ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\YourFileName.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True End Sub
Modify the text "Main Sheet" to match the exact name of the worksheet you want to export. Then, change "YourFileName.pdf" to your desired output file name.
Press F5 or click the green 'Run' button in the toolbar to execute the macro. The PDF will be generated with preserved hyperlinks and open automatically.

Automate PDF Exports Effortlessly with WPS Office
WPS Spreadsheet provides a robust VBA and Macro environment, allowing you to run your PDF export scripts seamlessly. It is a lightweight, fully compatible suite designed to handle advanced automated workflows effortlessly.
- 1. Install WPS Office: Download and install WPS Office Free on your computer.
- 2. Open Your Workbook: Launch WPS Spreadsheet and open your Macro-Enabled Workbook (.xlsm).
- 3. Access the VBA Editor: Navigate to the Tools tab and click on 'Macro', or simply press Alt + F11 to open the built-in VBA Editor.
- 4. Run Your Script: Insert a new module, paste your PDF export code, and run it directly to generate your interactive PDF.

Frequently Asked Questions
Why are my hyperlinks not clickable in the exported PDF?
This usually happens if 'IncludeDocProperties' is set to False in your VBA script, or if you attempt to use a virtual PDF printer (like Microsoft Print to PDF) instead of the ExportAsFixedFormat method. Ensure you use the specific VBA method described to retain hyperlink functionality.
How can I export the entire workbook to PDF instead of a single worksheet?
To export the whole workbook, change the object from the specific worksheet to the workbook itself in your VBA code. Replace 'ws.ExportAsFixedFormat' with 'ThisWorkbook.ExportAsFixedFormat' using the same parameters.
Can I specify a different output folder for the generated PDF?
Yes, you can modify the 'Filename' parameter in the script. Instead of using 'ThisWorkbook.Path', you can hardcode a specific directory path, such as 'Filename:="C:\Users\YourName\Documents\YourFileName.pdf"'.




