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
Sample files
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.
Open PDF File With VBA
Export Excel Charts As TIFF images Using Adobe Professional
VBA Macro To Convert PDF Files Into Different Format
Hi, Robert,
The methods shown here should work with 64bit systems.
I just tried the sample spreadsheet in Office 365 (64 bit) and the PDF file opened on the desired page.
Best Regards,
Christos
The most recent version is only for 32 bit systems. How to update it for 64 bit systems
Jean-Sébastien,
Another approach for opening a PDF file with VBA you can find here.
Maybe it is even better than the one presented here…
Kind Regards,
Christos
Thanks for the code Jean-Sébastien Côté.
One quick idea that I had is that it could be possible to open the PDF to a specific page by using API functions. Maybe this weekend if I find some time I will try to try it.
Kind Regards,
Christos
For those who want to copy the OpenPDF() macro, you will need to change these 2 lines:
vCommand = vAdobe & " /n /A ""page=" & vPage & """ """ & vDocument & ""
result = Shell(vCommand, vbNormalFocus)
The variable vCommand had an 'e' at the end in the original post. I miss that one when I translated the code from French to English.
Have a nice day!!
Richkotite1, in which version of Acrobat you are referring to? In Professional version I think it is possible, although little tricky. In Reader it might be even impossible. Since I haven’t installed the Reader version on my computer I cannot remember if it has the ability to create custom text boxes. But, one way or another, the Adobe Acrobat xx.0 Type Library can be used only with Professional, so you cannot automate the Reader using VBA. Regarding Professional, if I find some time I will try to write some VBA code for numbering pages.
Jean-Sébastien Côté, I will see the code and I will try to answer you as soon as possible… I hope to find a workaround.
Warm Regards to both,
Christos
Christos,
I tried changing the preference but still have the same problem. It's working once in a while. My file has a table that contain 3 columns. The first column is a description, the 2nd is the pdf file name (with or without the .pdf extension) and the 3rd one is the page number where the information is located in that pdf file.
I use code suggested to me that can be found at :
http://stackoverflow.com/questions/16068594/how-to-open-a-pdf-file-at-a-specifc-page-with-information-in-a-word-table
I added some validations to handle some possible error and here is the code I ended up with. It is working just fine but will need modifications when a new version of Acrobat Reader will be installed (or if I use it on another computer that doesn't have the same version or path for the Reader program) :
Sub OpenPDF()
' OpenPDF Macro
Dim vNumbers As String
Dim vAdobe As String
Dim aRow As row
Dim vDocument As String
Dim vPage As String
Dim vLen As Long
Dim vLoop As Long
Dim vPosFind As Integer
Dim vCommand As String
Dim result As Long
' String that contains only numbers
vNumbers = "0123456789"
' Full path for Acrobat Reader
vAdobe = "C:Program FilesAdobeReader 11.0ReaderAcroRd32.exe"
' Cursor must be in a table to continue
If Selection.Information(wdWithInTable) = True Then
Set aRow = Selection.Range.Rows(1)
' Remove weird characters put by Word in the cell for the document name
vDocument = Trim(Replace(Replace(aRow.Cells(2).Range.Text, " ", ""), vbCr, ""))
' Add ".pdf" at the end of the file name if not specified
vLen = Len(Trim(vDocument))
If vLen > 0 Then
If vLen > 5 And InStr(vLen – 4, StrConv(vDocument, vbLowerCase), ".pdf") = 0 Then
vDocument = vDocument & ".pdf"
End If
' If the file name is not specified, nothing is done
Else
Exit Sub
End If
' Add the path to the file name (pdf must be in the same folder as the word document)
vDocument = ThisDocument.Path & "" & vDocument
' Remove weird characters for the page number
vPage = Trim(Replace(Replace(aRow.Cells(3).Range.Text, " ", ""), vbCr, ""))
' If the page is not specified, set it to 1 (it is also working if blank)
If vPage = "" Then
vPage = "1"
Else
' Check that all characters are numbers
vLen = Len(Trim(vPage))
For vLoop = 1 To vLen
If InStr(vLoop, vNumbers, Mid(vPage, vLen, 1)) = 0 Then
MsgBox "The page number must be numeric"
Exit Sub
End If
' Check the next character
Next vLoop
' If the result is zero, change it to 1 to avoid an error by Acrobat Reader
If CLng(vPage) = 0 Then
vPage = "1"
End If
End If
' Open the pdf document at a specific page
' /n to open the file in a new instance of Acrobat Reader
' /A to indicate that an action will follow in our case it's the "page="
vCommande = vAdobe & " /n /A ""page=" & vPage & """ """ & vDocument & ""
result = Shell(vCommande, vbNormalFocus)
Else
' Error that the cursor must be in a table to do this macro.
MsgBox "The macro to open a PDF can only be run when the cursor is in a table"
End If
End Sub
If you have another way to call the AcroRd32.exe program without having to specified the full path, it will make this code usable on any PC with Acrobat Reader on it.
Thank you.
Very interesting site, is it possible to open the PDF, go to a specific page, then write the page Number for instance in the lower right hand corner. We get a bunch of reports that we create PDF's but we have to manually add page nums….we are looking for a way to automate it. Possible?
Dear Jean-Sébastien,
First of all thank you for informing me about the function bug. To tell you the truth I didn't notice the malfunction since I use the VBA code for Adobe Professional.
As you can see I have included a workaround to this problem, so the function should work normally.
Thank you for your contribution to this post.
Warm Regards,
Christos
Thanks Christos.
I tried the function since I don't have the Pro version, and it's opening the pdf file.
However, Adobe seems to know better than us where the document should open when you try to reopen the file again. It must be stored in cache somewhere because the pdf doesn't open at the requested page every time.
Also, when the document is already open, it does not switch to the Acrobat Reader application but adds characters were my cursor is in the page.
Any Idea how to solve that issue.
Thanks
Peter, here you can find the VBA code that I promised.
Warm Regards,
Christos