Get Executable path

A while ago I was developing an addin that utilised Winzip and realised that not every one puts the Winzip exe file (or even uses winzip, perhaps opting for another zip utility) in the default setup location, so I needed to come up with a way to get this.
eg on my system the exec file or default application to open it……..
C:Program FilesWinZipWINZIP32.EXE

This is what I came up with ….. I’d be interested in other ways you may have to get the files asscociated exec
program & location, possibly using the Registry, let us all know.

So if you ever need to know the default or current user setup application and location to run a particular document then give this a try.

Just plug in the file extension or the path to a document to get the executable file & location that opens it.

Try plugging in your own to see what you get………….

PROS: Utilises core API so every Win32 OS should be able to run this.
CONS: It gets the ASSOCIATED Application i.e what ever the user has set-up to run a particular file extension
so don’t assume they run the same application as you. i.e if you expect them to be using winzip then
check the associated program before utilising your code that may depend on winzip.

I have left the extraction of the various elements of “Path only” & “Executable only” up to you. There are a number of ways out there to get these elements :)

Option Explicit
‘—————————————————————————————
‘ Module    : basGetExe
‘ DateTime  : 05/12/04 19:18
‘ Author    : Ivan F Moala
‘ Purpose   : Gets the path to the Executable file as defined
‘           : in the associated files Extension
‘—————————————————————————————

Private Declare Function FindExecutable _
    Lib “shell32.dll” _
        Alias “FindExecutableA” ( _
            ByVal lpFile As String, _
            ByVal lpDirectory As String, _
            ByVal lpResult As String) _
As Long

Private Const MAX_PATH = 260

‘//—————————————————————————————
‘// Project    : VBAProject
‘// DateTime   : 05/12/04 21:07
‘// Author     : Ivan F Moala
‘// Site       : http://www.xcelfiles.com
‘// Purpose    : Gets Executable file path
‘// In         : [File extension] OR [Path to File]
‘//            : Note: Path to File doesn’t need to exist
‘//            : as it is just the extension we are after
‘// Out/Return : Short Path notation to executable file
‘—————————————————————————————

Public Function fnGetExePath(strFileExt_OrPathToFile As String) As String
‘// NB: I included Error lines JustInCase !
‘// Take them out + Errh, I used them for debugging
   Dim lRet As Long, strBuffer As String
    Dim strFn As String, strExt As String
    Dim handle As Integer
    Dim Pos As Long
   
    Pos = InStr(1, strFileExt_OrPathToFile, “.”)
    If Pos = 0 Then
        strExt = strFileExt_OrPathToFile
    Else
        strExt = Right(strFileExt_OrPathToFile, Len(strFileExt_OrPathToFile) – Pos)
    End If
   
    strFn = fnTmpFolderLocation & “zTmp.” & strExt
    handle = FreeFile

    On Error GoTo Errh
    ‘// Create a new File to use as our reference
1   Open strFn For Output As #handle
    ‘// Write to File
2   Print #handle, vbNullString
    ‘// Close the File
3   Close #handle
   
    ‘// Create a buffer to hold the string path
4   strBuffer = String(MAX_PATH, 32)
    ‘// Retrieve the name and handle of the executable, associated with this file
5   lRet = FindExecutable(strFn, vbNullString, strBuffer)
6   If lRet > 32 Then
        ‘// Found!
7       fnGetExePath = Application.WorksheetFunction.Clean(strBuffer)
    Else
        ‘// NOT found
8       fnGetExePath = vbNullString
    End If
   
    ‘// Delete the File
9   Kill strFn

Exit Function
Errh:
    fnGetExePath = “An Error occured! “ & ” @Line “ & Erl() & vbCrLf & _
        “ErrNumber “ & Err.Number & “:=” & Err.Description
   
End Function

Function fnTmpFolderLocation() As String
‘//—————————————————————————————
‘// Project   : VBAProject
‘// DateTime  : 05/12/04 21:19
‘// Author    : Ivan F Moala
‘// Site      : http://www.xcelfiles.com
‘// Purpose   : Gets a Temp directory location to work in
‘// In        : None
‘// Out/Return: Temp Directory either Tmp or default Workbook path
‘//—————————————————————————————
Dim Tmp As String, Fso As Object, TFolder As Object

‘// 1st Try getting via Environ
Tmp = Environ(“Tmp”)
If Len(Tmp) <> 0 Then GoTo Xit

‘// NoGo so try FSO
Set Fso = CreateObject(“Scripting.FileSystemObject”)
Set TFolder = Fso.getSpecialFolder(2)
Tmp = TFolder.Path

If Len(Tmp) = 0 Then
    ‘// Still No Go so use This workbooks path
   ‘// as long as it’s saved.
   fnTmpFolderLocation = ThisWorkbook.Path
End If

Set Fso = Nothing
Set TFolder = Nothing

Exit Function
Xit:
fnTmpFolderLocation = Tmp

End Function

Sub Tester()
Dim Msg As String
Dim aExt As Variant
Dim i As Integer
Dim Tmp As String

Const strNoAss As String = “No associated program”

‘// Lets test these file Extensions… try your own
aExt = Array(“hlp”, “.gif”, “hta”, “url”, “Dao350.dll”, “doc”, “.pdf”, “zzzasda”)
i = 0
     
Do Until i > UBound(aExt)
    ‘// Do the job, giving appropriate reply
   Tmp = fnGetExePath(CStr(aExt(i)))
    Tmp = IIf(Tmp <> vbNullString, Tmp, strNoAss)
    ‘// Build str message
   Msg = “The current application associated with File extension:= “
    Msg = Msg & aExt(i) & vbCrLf & vbCrLf
    Msg = Msg & “Is located @” & vbCrLf
    MsgBox Msg & vbCrLf & Tmp, vbInformation, “Exercutable path”
    i = i + 1
Loop

End Sub

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *