Monday, 23 January 2017

VBA get Word Excel Outlook Path without reference and declaration, late binding

Hi All, here an example to know Word, Excel or Outlook .exe path, without adding reference to object
Here the function, copy and paste it to vba module

Function GetProgFilesPath(ByVal sExecutable As String) As String
    On Error GoTo Err_Exit
    Dim obj As Object
    Select Case LCase(sExecutable)
        Case "winword.exe", "word.exe"
            Set obj = CreateObject("Word.Application")
        Case "excel.exe"
            Set obj = CreateObject("Excel.Application")
        Case "outlook.exe"
            Set obj = CreateObject("Outlook.Application")
        Case Else
            MsgBox "Not Managed" & sExecutable, vbInformation
            Exit Function
        End Select
    GetProgFilesPath = obj.path
    Set obj = Nothing

    Exit_Exit:
    Exit Function
    Err_Exit:
        MsgBox "Err:GetProgFilesPath=" & Err.Description
    Resume Exit_Exit
End Function

No comments:

Post a Comment