Академический Документы
Профессиональный Документы
Культура Документы
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
Home
Blog Contents
About
ERTAT Add-In
Work With Me
Advertise Here
Suggested Books
Disclaimer
10
Like
We're on
Follow @MyEnginWorld 19 followers
2
Your e-mail address...
Tweet
Submit
Members (16)
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 specific 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 challenge part was to use the PDF objects from VBA, so I searched on Adobe SDK to find the vocabulary that uses Adobe on their programs (Acrobat Reader/Pro). I ended up with a VBA macro that can be used from any office application (Word, Power Point & Excel), since it is free from application-specific objects (i.e. sheets). Note that you should have installed Adobe Acrobat Professional to your computer in order to use this macro. Excel VBA Read And Write Text Files VBA Macro To Open A PDF File VBA Macro To Convert PDF Files Into Different Format Draw A Polyline In AutoCAD Using Excel VBA Open A Google Earth File (kmz) With Google Maps Update 19/9/2012: However, there is a way to open a PDF from an Office application even with Adobe Reader. Fatigue Analysis In Turbomachinery Solutions Of Colebrook & White Equation VBA code AutoCAD Calculate The Total Length Of Lines Open Channel Flow - Trapezoidal Cross Section
Update 30/4/2013: A more generic VBA code that works with both Adobe Reader and Professional can be found here.
Option Explicit
1 of 7
10/9/2013 4:54 PM
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
Option Private Module Sub OpenPDFPageView() 'By Christos Samaras 'http://www.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 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.p '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
2 of 7
10/9/2013 4:54 PM
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
PDFApp.Show 'Set the focus to adobe acrobat pro AppActivate "Adobe Acrobat Pro" End Sub
VBA results
I received some e-mails from people asking me if is possible to open a PDF file using Adobe Reader. Well, it is possible, but the "Sendkeys" method must be used. 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
3 of 7
10/9/2013 4:54 PM
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
Although this function works, the "sendkeys" method has a serious shortcoming: when the macro runs, the user must not use the keyboard because is possible to corrupt it. So, to sum up, if you have Adobe Professional installed to your computer use the first macro, but if you have Adobe Reader use the function.
In seems that the last update of the Adobe Reader (and Professional) has changed one important setting and since then the function doesn't working. I am referring to the "Restore last view settings when reopening documents". As Jean-Sbastien wrote on the comments the macro acts strangely. However, the workaround on this problem is quite easy.
2) Choose the Document tab and uncheck the option: Restore last view setting when reopening documents.
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 to any office application you want. 2. A Word document, a Power Point 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).
4 of 7
10/9/2013 4:54 PM
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
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 enjoy this post? If yes, then like and share it!
Like
10
Tweet
StumbleUpon
Christos Samaras
Mechanical Engineer, M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece Communication: tel. +30-6973513308, e-mail , Facebook , Twitter , Google+ and Lnkedin.
Newer Post
Home
Older Post
5 of 7
10/9/2013 4:54 PM
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
31 comments
Newest
Community
Share
Avatar
mac
any idea how to import xml data into an acrobat form. with the controls it's just tools->more form options -> manage form data -> import data
Avatar Christos Samaras Admin What exactly do you want to do Mac? Read an xml file via code?
Avatar mac i'd like the PDF form to import data (.xml file) which would update various nodes within the form
1
Avatar
rik
after few days of surfing the internet, the solution in a few VBA lines. excellent, many thanks
Avatar Christos Samaras Admin You 're welcome rik, I am glad that it help you! Kind Regards, Christos
Avatar
Jesus
Avatar Christos Samaras Admin You're welcome! Thank you for your comment.
Avatar
Christos Samaras
Jean-Sbastien, 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
Avatar
Christos Samaras
Thanks for the code Jean-Sbastien Ct. 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
Avatar
Jean-Sbastien Ct
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)
6 of 7
10/9/2013 4:54 PM
http://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-fi...
7 of 7
10/9/2013 4:54 PM