Link to home
Start Free TrialLog in
Avatar of Mark Epstein
Mark EpsteinFlag for United States of America

asked on

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

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
    Else
        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)
            
                DoEvents
                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
            Wend
            
        End If
        
    End If
a:
 
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

ASKER CERTIFIED SOLUTION
Avatar of mrfixit22
mrfixit22

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark Epstein

ASKER

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??  
Excellent solution. Thanks!