How to Open a PDF Using VBA (With Example)


You can use the FollowHyperlink method in VBA to open a PDF file from a specific file path.

Here is one common way to use this method in practice:

Sub OpenPDF()
ActiveWorkbook.FollowHyperlink "C:\Users\bob\Documents\basketball_data.pdf"
End Sub

This particular macro opens the pdf called basketball_data.pdf located in a specific folder on my computer.

By default, the PDF file will be opened using the default PDF reader on your own computer.

The following example shows how to use this syntax to read a text file in practice.

Example: How to Open a PDF Using VBA

Suppose we have a PDF file called basketball_data.pdf located at the following file path:

C:\Users\bob\Documents\basketball_data.pdf

Suppose we would like to open this PDF using VBA.

We can create the following macro to do so:

Sub OpenPDF()
ActiveWorkbook.FollowHyperlink "C:\Users\bob\Documents\basketball_data.pdf"
End Sub

When we run this macro, we may receive the following Microsoft Excel Security Notice that simply lets us know this location may be unsafe and asks if we’d like to proceed anyway:

Once we click Yes, the PDF will then be opened:

This particular PDF simply contains a dataset about basketball players on various teams.

Note: You can find the complete documentation for the FollowHyperlink method here.

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

How to Open a Text File Using VBA
How to Create Folders Using VBA
How to Delete Folders Using VBA
How to Delete Files Using VBA

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *