Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Open pdf file from excel vba?

This is a discussion on Open pdf file from excel vba? within the Excel Questions forums, part of the Question Forums category; I'm trying to open a pdf file from within excel vba . I have tried using the followhyperlink method but ...

  1. #1
    Board Regular tinkythomas's Avatar
    Join Date
    Dec 2006
    Location
    Southampton
    Posts
    432

    Default Open pdf file from excel vba?

    I'm trying to open a pdf file from within excel vba. I have tried using the followhyperlink method but adobe acrobat opens very briefly then immediately closes
    Code:
    Sub OpenPDF()
    
    'Dim pdf As String
    
        On Error Resume Next
    
        'pdf file to open
        pdf = "K:\PDF\mypdf.pdf"
    
        'open the pdf file
        ActiveWorkbook.FollowHyperlink pdf
    
    End Sub
    So then I tried to create an instance of acrobat by setting a reference to the acrobat object but I can't get this to work either!

    The code I'm using is
    Code:
    Sub OpenPDF()
    
        Dim pdf As AcroPDDoc
        Dim strPDF As String
    
        Set pdf = CreateObject("AcroExch.PDDoc")
        
        'pdf file to open
        strPDF = "K:\PDF\mypdf.pdf"
    
        'open the pdf file
        pdf.Open strPDF
    
    End Sub
    Any ideas what could be wrong with either approach?

    Thank-you
    Chris
    _________________
    Office 2003/2007/XP SP3/Win 7 Ultimate

  2. #2
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Default Re: Open pdf file from excel vba?

    Hi tinkythomas ;

    Code:
     Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Code:
    Sub EVN()
    Filename = Application.DefaultFilePath & "\myPDF.Pdf"
    ShellExecute 0, "Open", Filename, "", "", vbNormalNoFocus
    End Sub

  3. #3
    Board Regular tinkythomas's Avatar
    Join Date
    Dec 2006
    Location
    Southampton
    Posts
    432

    Default Re: Open pdf file from excel vba?

    Hi Tarkan, where do I place the first line sof code? I'm receiving a compile error with the code as displayed. The error is "Only comments may appear after End Sub, End Function or End Property"

    Sorry my vba skills are still poor!

    Thanks
    Chris
    _________________
    Office 2003/2007/XP SP3/Win 7 Ultimate

  4. #4
    Board Regular tinkythomas's Avatar
    Join Date
    Dec 2006
    Location
    Southampton
    Posts
    432

    Default Re: Open pdf file from excel vba?

    Ok, I have now placed the code in a new module and have eliminated the compile error! But the pdf file doesn't open. I have systernals process explorer to monitor whether acobat.exe launches and it does but then immediately closes

    Any ideas??
    Last edited by tinkythomas; Oct 28th, 2008 at 06:16 PM.
    Chris
    _________________
    Office 2003/2007/XP SP3/Win 7 Ultimate

  5. #5
    Board Regular JP2112's Avatar
    Join Date
    Oct 2008
    Location
    NYC
    Posts
    237

    Default Re: Open pdf file from excel vba?

    Try this:

    Code:
    Dim myShell As Object
    Set myShell =  CreateObject("WScript.Shell")
    myShell.Run "K:\PDF\mypdf.pdf"
    --JP

  6. #6
    Board Regular tinkythomas's Avatar
    Join Date
    Dec 2006
    Location
    Southampton
    Posts
    432

    Default Re: Open pdf file from excel vba?

    Thanks JP, it appeared that my acrobat pro install was corrupt! Although I could open pdf files normally by double clicking

    Anyway, I uninstalled acrobat pro and installed the latest reader and now I can open pdf's using the followhyperlink method.

    Not sure what was wrong with the pro install, maybe I will re-install and have another go? But for now with the reader all appears to be well.

    Thanks for evryone's help it has been a great help.
    Chris
    _________________
    Office 2003/2007/XP SP3/Win 7 Ultimate

  7. #7
    Board Regular JP2112's Avatar
    Join Date
    Oct 2008
    Location
    NYC
    Posts
    237

    Default Re: Open pdf file from excel vba?

    That's usually the cause when the usual methods of opening PDFs stop working. Glad to hear it worked out!

    --JP

  8. #8
    New Member
    Join Date
    Oct 2008
    Posts
    7

    Default Re: Open pdf file from excel vba?

    Hi again,
    Alternative code example
    Code:
     
    Sub RunPDFWithExe()
    Shell "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe C:\Documents and Settings\user\Desktop\Traveller_ypk_full.pdf", vbNormalFocus
    End Sub
    or

    Code:
    Sub RunPDFWithExe()
    MyPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"
    MyFile = "C:\Documents and Settings\user\Desktop\Traveller_ypk_full.pdf"
    Shell MyPath & " " & MyFile, vbNormalFocus
    End Sub
    Please change this Mypath and MyFile ...


    or look at this example movie
    http://www.excelvba.net/forum/RunPdf.html

  9. #9
    Board Regular tinkythomas's Avatar
    Join Date
    Dec 2006
    Location
    Southampton
    Posts
    432

    Default Re: Open pdf file from excel vba?

    Tarkan VURAL, thank-you for the reply. I'm sorry it's taken so long for me to reply, have been very busy recently.

    Thanks for the revised code, this works perfectly. I now have several methods for opening pdf files.

    Thanks
    Chris
    _________________
    Office 2003/2007/XP SP3/Win 7 Ultimate

  10. #10
    New Member
    Join Date
    Nov 2008
    Posts
    1

    Smile Re: Open pdf file from excel vba?

    What command would you use for doing something similar with PDF but printing instead of opening? I have a very similar application to tinkythomas but printing is my objective, not opening. Thoughts?

Page 1 of 2 12 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com