Opening files in other programs


Q: How can I print an Adobe Acrobat file from within Excel? 

A. You can achieve that by using the ShellExecute API procedure

Create a user form as shown below and enter the following code 


Option Explicit

'Declare the two API procedures that we want to use

'ShellExecute either opens or prints a file, using the programme that
'is registered for the filetype in question
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

'GetActiveWindow returns a window handle for the active window.
'This handle is required for ShellExecute to work.
Private Declare Function GetActiveWindow Lib "user32" () As Long

'Declare two constants to use with ShellExecute
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_HIDE = 0

Dim lnghWnd As Long 'Variable to hold the window handle value
Dim lngShow As Long 'Variable to hold the windowstate for Adobe


Private Sub cmdExecute_Click()
  'Variable used in ShellExecuteto decide whether file is shown or printed
 
Dim strPurpose As String

  If optPrint.Value = True Then 'User wants to print file
    strPurpose = "Print"
    lngShow = SW_HIDE
  Else                           'User wants to view file
    strPurpose = "Open"
    lngShow = SW_SHOWMAXIMIZED
  End If

  'Call the ShellExecute procedure
 
ShellExecute lnghWnd, strPurpose, txtFile.Text, vbNullString, txtFolder.Text, lngShow

End Sub
Private Sub UserForm_Initialize()

  'Find the handle to the active window (in this case Excel)
 
lnghWnd = GetActiveWindow

  'The following two statements just used to simplify testing
 
txtFolder.Text = "D:\Data\VBDok\PDF"
  txtFile.Text = "Test.pdf"

End Sub