How can I open a PDF using VBA, and do you have an example of how to do so?

Opening a PDF using VBA (Visual Basic for Applications) can be achieved by using the “Shell” function. This function allows the user to run an external program from within VBA. To open a PDF file, the user can specify the path of the PDF reader application and the path of the PDF file as parameters for the Shell function. The example code below demonstrates how to open a PDF using VBA:

Sub OpenPDF()
Dim pdfReaderPath As String
Dim pdfFilePath As String

‘Specify the path of the PDF reader application
pdfReaderPath = “C:Program Files (x86)AdobeAcrobat Reader DCReaderAcroRd32.exe”

‘Specify the path of the PDF file to be opened
pdfFilePath = “C:DocumentsExample.pdf”

‘Use the Shell function to open the PDF file
Shell pdfReaderPath & ” ” & pdfFilePath, vbNormalFocus
End Sub

This code will open the PDF file using the specified PDF reader application. The “vbNormalFocus” parameter ensures that the PDF reader application is brought to the front and given focus. This is just one example of how to open a PDF using VBA, and the code can be modified to suit different needs and preferences.

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

Additional Resources

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

x