[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2498
  • Last Modified:

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

0
M_Epstein
Asked:
M_Epstein
  • 2
1 Solution
 
mrfixit22Commented:
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_Test:
Exit Function

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

End Function
0
 
M_EpsteinAuthor Commented:
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??  
0
 
M_EpsteinAuthor Commented:
Excellent solution. Thanks!
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now