Shell "Start.exe ""D:\My Documents\sm569_Jan2001\tute6.pdf"""
Start.exe isn't available on all platforms -- at least that has been my
experience on Windows XP Pro and Vista. For example, I don't have it here
on my laptop with Windows Vista Ultimate (Shell errors with "53 - File Not
Found").
Instead, you can use FindExecutable, which will work on any platform, to
find the exe file associated with the file and Shell to that exe passing the
file name to the exe. Note that the file passed in to FindExecutable must
exist. If it doesn't FindExecutable will fail.
A result >= 32 from FindExecutable indicates success. Change the value of
PDFFileName to the fully-qualified name of your PDF file.
Public Declare Function FindExecutable Lib "shell32.dll" _
Alias "FindExecutableA" ( _
ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal lpResult As String) As Long
Const C_MIN_FINDEXE_SUCCESS_VALUE = 32
Const MAX_PATH = 260
Sub ShellToExe()
''''''''''''''''''''''''''''''''''''
' Shells to the exe associated with
' PDFFileName and opens PDFFileName.
''''''''''''''''''''''''''''''''''''
Dim ExeName As String
Dim Pos As Integer
Dim PDFFileName As String
Dim Res As Long
' allocate space in ExeName variable.
ExeName = String$(MAX_PATH, vbNullChar)
'>>>>> CHANGE FILE NAME
PDFFileName = "C:\Whatever\FileName.pdf" '<<< File MUST exist
If Dir(PDFFileName, vbNormal) = vbNullString Then
MsgBox "File: " & PDFFileName & " does not exist." & vbCrLf & _
"'FindExecutable' requires an existing file name."
Exit Sub
End If
' Get the exe name associated with "pdf".
Res = FindExecutable(PDFFileName, vbNullString, ExeName)
If Res >= C_MIN_FINDEXE_SUCCESS_VALUE Then
' trim off trailing vbNullChar characters
Pos = InStr(1, ExeName, vbNullChar, vbBinaryCompare)
If Pos Then
ExeName = Left(ExeName, Pos - 1)
End If
Shell Chr(34) & ExeName & Chr(34) & Chr(32) & _
Chr(34) & PDFFileName & Chr(34),vbMaximizedFocus
Else
MsgBox "Error From FindExecutable: " & CStr(Res)
End If
End Sub
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email on the web site)