How can I open a PDF file through VBA?


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:UsersbobDocumentsbasketball_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:UsersbobDocumentsbasketball_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:UsersbobDocumentsbasketball_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.

x