You are on page 1of 6

VBA Macro To Open A PDF File

Written by Christos Samaras on Tuesday, 31 July 2012 at 23:30


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 VBAmacro 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.
Update 19/9/2012: However, there is a way to open a PDF from an Office application even with Adobe Reader.
RSS Feed RSS Feed Twitter Twitter Google Plus Google Plus
217 people like this. Sign Up to see what your
friends like.
Like Like
My Engineering World
+ 49
Follow +1
Follow Follow @MyEnginWorld @MyEnginWorld 29 followers
Your e-mail address... Submit Submit
Please Join Our New Linkedin Group
Professional Excel Development
Search this blog...
Enterprise Web
Hardware
solutions.radware.com
Create Loyalty By Beating Customer
Web Performance Demands. See
How!
2
3
3
11
Like Like
Home Blog Contents About Work With Me ERTAT Add-In Advertise Here Suggested Books Disclaimer
converted by Web2PDFConvert.com
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
'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\ACADM2010\Setup\en-US\SetupRes\Docs\Acad_Mech_2010_UserGuid
e.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)

'-------------
'ZOOMoptions
'-------------
'0 = AVZoomNoVary
'1 = AVZoomFitPage
'2 = AVZoomFitWidth
'3 = AVZoomFitHeight
Top 10 Stories
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
Numerical Integration In Excel Using The Trapezoidal
Rule
Welding Discontinuities X-ray Films
Open A Google Earth File (kmz) With Google Maps
Fatigue Analysis In Turbomachinery

Open A PDF File With VBA
converted by Web2PDFConvert.com
'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
VBA results
Update 19/9/2012: VBA code for Adobe Reader
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 VBAfunction below. Have in mind that this function also
works with Adobe Professional.
converted by Web2PDFConvert.com
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 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.
Update 22/3/2013: Fix the function bug for Adobe Reader
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.
1) Go to Edit > Preferences in your Adobe PDF Reader.
2) Choose the Document tab and uncheck the option: Restore last view setting when reopening documents.
converted by Web2PDFConvert.com
3
3) Press OK and that's it! The function will work again.
Update 30/4/2013: A better approach
Amore generic VBAcode 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. AWord 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).
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!
11 Like Like Tweet Tweet 2 3
Categories: Office Tips
Edit & Convert PDF
Files
free-download.iskysoft.us
All-in-One PDF Tool on Windows&
Mac Edit,Convert, Annotate PDF
Files.
converted by Web2PDFConvert.com
Copyright 2011 - 2014: Copyright 2011 - 2014: Christos Samaras Christos Samaras Follow us on Follow us on Facebook Facebook , , Twitter Twitter , , Google+ Google+ & & Linkedin Linkedin
Newer Post Older Post
Christos Samaras
Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: tel. +30-6973513308, e-mail , Facebook , Twitter , Google+ and Linkedin . Full CV here.
Home
converted by Web2PDFConvert.com

You might also like