Tuesday, 31 July 2012

VBA Macro To Open A PDF File

The previous week, while I was preparing a presentation, I encountered the following problem: how could I open a PDF file from a power point presentation? And more precisely, how could I open a PDF file to a particular page (i.e., 46) and view that page with a specific view (i.e., fit page)? I managed to solve this problem using VBA. The challenging part was to use the PDF objects from VBA, so I searched for Adobe SDK to find the “vocabulary” that uses Adobe in their programs (Acrobat Reader/Pro). I ended up with a VBA macro that can be utilized from any office application (Word, PowerPoint & Excel) since it is free from application-specific objects (i.e., sheets). Note that you should have installed Adobe Acrobat Professional on your computer to use this macro.  


Update 19/9/2012: However, there is a way to open a PDF from an Office application even with Adobe Reader.

Update 30/4/2013: A more generic VBA code that works with both Adobe Reader and Professional can be found here.

VBA code

Option Explicit
Option Private Module

Sub OpenPDFPageView()
    'By Christos Samaras
    'In order to use the macro you must enable the Acrobat library from VBA editor:
    'Go to Tools -> References -> Adobe Acrobat xx.0 Type Library, where xx depends
    'on your Acrobat Professional version (i.e. 9.0 or 10.0) you have installed to your PC.
    'Alternatively you can find it Tools -> References -> Browse and check for the path
    'C:\Program Files\Adobe\Acrobat xx.0\Acrobat\acrobat.tlb
    'where xx is your Acrobat version (i.e. 9.0 or 10.0 etc.). 
    Dim PDFApp As AcroApp
    Dim PDFDoc As AcroAVDoc
    Dim PDFPageView As AcroAvPageView
    Dim PDFPath As String
    Dim DisplayPage As Integer
    'Change this to your own complete PDF path
    'Full path example
    'PDFPath = "C:\Program Files\Autodesk\ACADM 2010\Setup\en-US\SetupRes\Docs\Acad_Mech_2010_UserGuide.pdf"
    'For Word
    'PDFPath = ThisDocument.Path & "\" & "PDF Sample.pdf"
    'For Power Point
    'PDFPath = ActivePresentation.Path & "\" & "PDF Sample.pdf"
    'For Excel
    PDFPath = ThisWorkbook.Path & "\" & "PDF Sample.pdf"
    'Set the page you want to be displayed
    DisplayPage = 3
    'Initialize Acrobat by creating App object
    Set PDFApp = CreateObject("AcroExch.App")
    'Set AVDoc object
    Set PDFDoc = CreateObject("AcroExch.AVDoc")
    'Open the PDF
    If PDFDoc.Open(PDFPath, "") = True Then
        'Maximize the document
        Call PDFDoc.Maximize(True)
        Set PDFPageView = PDFDoc.GetAVPageView()
        'Go to the desired page
        'The first page is 0
        Call PDFPageView.GoTo(DisplayPage - 1)
        'ZOOM options
        '0 = AVZoomNoVary
        '1 = AVZoomFitPage
        '2 = AVZoomFitWidth
        '3 = AVZoomFitHeight
        '4 = AVZoomFitVisibleWidth
        '5 = AVZoomPreferred
        'Set the page view of the pdf
        Call PDFPageView.ZoomTo(2, 50)
    End If

    Set PDFApp = Nothing
    Set PDFDoc = Nothing
    On Error Resume Next
    'Show the adobe application
    'Set the focus to adobe acrobat pro
    AppActivate "Adobe Acrobat Pro"
End Sub 

Demonstration video

Update 19/9/2012: VBA code for Adobe Reader

I received some e-mails from people asking me if it is possible to open a PDF file using Adobe Reader. Well, it is possible, but the "Sendkeys" method must be employed. See the VBA function below. Have in mind that this function also works with Adobe Professional.

Option Explicit

Function OpenPDFPage(PDFPath As String, PageNumber As Long, PageView As Integer)
    'Opens a pdf file, at specific page and with specific view.
    'Sendkeys method is used for simulating keyboard shortcuts.
    'It can be used with both Adobe Reader & Adobe Professional.
    'By Christos Samaras
    'This line depends on the apllication you are using.
    'For Word
    'ThisDocument.FollowHyperlink PDFPath, NewWindow:=True
    'For Power Point
    'ActivePresentation.FollowHyperlink PDFPath, NewWindow:=True
    'For Excel
    ThisWorkbook.FollowHyperlink PDFPath, NewWindow:=True
    SendKeys ("^+N" & PageNumber & "~^" & PageView), True

End Function

Sub Test()

    OpenPDFPage "C:\Test.pdf", 115, 2
    'Page view options:
    '0: Full Page
    '1: Zoom to 100%
    '2: Page Width

End Sub

Although the above function works, the "sendkeys" method has a severe shortcoming: when the macro runs, the user must not use the keyboard because it is possible to corrupt the function. So, to sum up, if you have Adobe Professional installed on your computer use the first macro, but if you have Adobe Reader use the function.

Update 22/3/2013: Fix the function bug in Adobe Reader

It seems that the last update of the Adobe Reader (and Professional) has changed one important setting and since then the function doesn't work. I am referring to the "Restore last view settings when reopening documents." As Jean-S├ębastien wrote in the comments, the macro acts strangely. However,  the workaround for this problem is quite easy.  

1) Go to Edit –> Preferences in your Adobe PDF Reader.

Adobe Preferences

2) Choose the Document tab and uncheck the option: “Restore last view setting when reopening documents.”

Adobe Last Page Viewed

3) Press OK, and that's it! The function will work again.

Update 30/4/2013: A better approach

A more generic VBA code that works with both Adobe Reader and Professional can be found here. It doesn't require reference to the Adobe Type Library!

Sample files

The rar file contains the following files:
  1.  A VBA module with the above code for Adobe Professional. You can import it into any office application you want.
  2. A Word document, a PowerPoint presentation and an Excel workbook that are used to demonstrate the usage of the same VBA code in different applications (see the video above).
  3.  A short PDF file that is opened by the above macro (for Adobe Professional).
  4.  Update 19/9/2012: a VBA module with the function for Adobe Reader (and Adobe Professional).

Download it from here

These files can be opened with Office 2007 or newer. Please, remember to enable macros before using them.

Read also

Open PDF File With VBA
Export Excel Charts As TIFF images Using Adobe Professional
VBA Macro To Convert PDF Files Into Different Format

Did you like this post? If yes, then share it with your friends. Thank you!


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail, Facebook, Twitter, Google+ and Linkedin. More info