www myengineeringworld net (15)

Upload: jhdmss

Post on 11-Oct-2015

22 views

Category:

Documents


0 download

TRANSCRIPT

  • VBA Macro To Open A PDF FileWritten 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 PDFfile from a power point presentation? And more precisely, how could I open a PDF file to a specific page (i.e. 46) andview that page with a specific view (i.e. fit page)? I managed to solve this problem using VBA. The challenge part was touse 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 AcrobatProfessional 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 FeedRSS Feed TwitterTwitter Google PlusGoogle Plus

    217 people like this. Sign Up to see what yourfriends like.

    LikeLike

    My Engineering World

    + 49

    Follow +1

    Follow Follow @MyEnginWorld@MyEnginWorld 29 followers

    Your e-mail address... SubmitSubmit

    Please Join Our New Linkedin Group

    Professional Excel Development

    Search this blog...

    Enterprise WebHardware

    solutions.radware.comCreate Loyalty By Beating CustomerWeb Performance Demands. SeeHow!

    2

    3

    3

    11

    LikeLike

    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 ExplicitOption 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.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

    Top 10 StoriesExcel 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 TrapezoidalRule

    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 ispossible, but the "Sendkeys" method must be used. See the VBA function below. Have in mind that this function alsoworks 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 mustnot use the keyboard because is possible to corrupt it. So, to sum up, if you have Adobe Professional installed to yourcomputer 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 sincethen the function doesn't working. I am referring to the "Restore last view settings when reopening documents". AsJean-Sbastien wrote on the comments the macro acts strangely. However, the workaround on this problem is quiteeasy.

    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

  • 33) 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 requirereference 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 ofthe 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 ProfessionalVBA Macro To Convert PDF Files Into Different Format

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

    11LikeLike TweetTweet 2 3

    Categories: Office Tips

    Edit & Convert PDFFiles

    free-download.iskysoft.usAll-in-One PDF Tool on Windows&Mac Edit,Convert, Annotate PDFFiles.

    converted by Web2PDFConvert.com

  • Copyright 2011 - 2014: Copyright 2011 - 2014: Christos SamarasChristos Samaras Follow us on Follow us on Facebook Facebook , , Twitter Twitter , , Google+ Google+ & & LinkedinLinkedin

    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