How to open a .pdf document using shell function of Adobe Acrobat Professional

Posted on 2009-02-21
Last Modified: 2013-11-12
I have vb code (see below) which allows me to programatically open a .pdf document using adobe reader. It works very well. I tried it using AAdobe Professional, but it would not work. Any ideas?
Private Const ADOBE_ACROBAT_OPEN = """C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"" ""%1"""

Private Const ADOBE_ACROBAT_PRINT = """C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe"" /p /h ""%1"""

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Dim Filename As String

Function Manage_PDF(Filename, WindowState As VbAppWinStyle, JustOpenAcrobat As Boolean, CloseAdobe As Boolean)


GoTo a

    If (JustOpenAcrobat) Then

        Shell Replace(ADOBE_ACROBAT_OPEN, "%1", Filename), WindowState


        Shell Replace(ADOBE_ACROBAT_PRINT, "%1", Filename), WindowState


        If (CloseAdobe) Then

            Dim hWnd As Long

            Dim iTry As Integer

            Dim bClosed As Boolean

            Dim bSentMessage As Boolean


            bClosed = False

            bSentMessage = False


            While (iTry < 1000 And Not bClosed)



                hWnd = FindWindow(vbNullString, "Adobe Reader")


                If (bSentMessage And hWnd = 0) Then

                    bClosed = True

                End If


                If (hWnd) Then

                    SendMessage hWnd, &H10, 0, ByVal 0

                    bSentMessage = True

                End If


                iTry = iTry + 1



        End If


    End If


End Function

When I tried to use the executable for Acrobat professional, nothing happened: (It lies on my D drive)

Private Const ADOBE_ACROBAT_OPEN = """D:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe"" ""%1"""

Private Const ADOBE_ACROBAT_PRINT = """D:\Program Files\Adobe\Acrobat 7.0\Acrobat\Acrobat.exe"" /p /h ""%1"""

Open in new window

Question by:M_Epstein
    LVL 5

    Accepted Solution

    Once you have the file names sorted out you can use the ShellExecute API function that uses the default application associated with the file extension: -

    Option Explicit
    Option Compare Text

    Public Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal lngHwnd As Long, _
    ByVal strOperation As String, _
    ByVal strFile As String, _
    ByVal strParameters As String, _
    ByVal strDirectory As String, _
    ByVal lngShow As Long) As Long

    Public Const conHide As Long = 0
    Public Const conShow As Long = 1

    Sub Test()

    ' To open the file...
    ShellExecute 0, "Open", "C:\brakes.pdf", "", "", conShow

    ' To print the file to the default output device...
    ShellExecute 0, "Print", "C:\brakes.pdf", "", "", conHide

    ' To execute an executable file...
    ShellExecute 0, "Open", "C:\RunCompact.bat", "", "", conHide

    End Sub
    No need for the application name which might change from version to version.

    Here is another variation

    Option Compare Database
    Option Explicit

    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

    Public Function Test()
    On Error GoTo Err_Test

    'Open file
    ShellExecute 0, "Open", "C:\Temp\123.txt", "", "", 1

    'Print file
    ShellExecute 0, "Print", "C:\Temp\123.txt", "", "", 0

    'Execute an executable
    ShellExecute 0, "Open", "C:\Temp\123.bat", "", "", 0

    Exit Function

    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Test

    End Function

    Author Comment

    Great function! Elegant, brief and it works!
    One final question: you showed me how to open and how to print. How do I close a document using this or a similar function??  

    Author Closing Comment

    Excellent solution. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: : Click on Too…
    The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now