The previous week, while I was preparing a presentation, I encountered the following problem: how could I open a PDF file from a PowerPoint 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 by 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.
Option Explicit Option Private Module Sub OpenPDFPageView() 'By Christos Samaras 'https://myengineeringworld.net '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 FilesAdobeAcrobat xx.0Acrobatacrobat.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\Setupen-US\Setup\ResDocsAcad_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 PDFDoc.BringToFront '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 PDFApp.Show 'Set the focus to adobe acrobat pro AppActivate "Adobe Acrobat Pro" End Sub
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. To sum up, if you have Adobe Professional installed on your computer, use the first macro. In case you have Adobe Reader, use the function.
Update 22/3/2013: Fix the function bug in Adobe Reader
Update 30/4/2013: A better approach
The RAR file contains the following files:
- A VBA module with the above code for Adobe Professional. You can import it into any office application you want.
- A Word document, a PowerPoint presentation, and an Excel workbook are used to demonstrate the same VBA code usage in different applications (see the video above).
- A short PDF file is opened by the above macro (for Adobe Professional).
- 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.