Friday 18 January 2019

VBA - How to tell if running in Excel or Word

Just a quick post. For a demo I wanted to run some code in Microsoft Word instead of Microsoft Excel. So I needed a method to determine in which environment the code was running. I have selected this code which probes the object model; it uses late binding to slyly evade compile errors.

UPDATE

Ignore code below, Application.Name returns "Microsoft Excel" in Excel and "Microsoft Word" in Word

deprecated...


Function ExcelVBA() As Boolean
    On Error GoTo QuickExit
    Dim objApp As Object
    Set objApp = Application
    
    Dim objWbs As Object
    Set objWbs = objApp.Workbooks
    
    ExcelVBA = True
QuickExit:
End Function

Function WinWordVBA() As Boolean
    On Error GoTo QuickExit
    Dim objApp As Object
    Set objApp = Application
    
    Dim objActiveDocument As Object
    Set objActiveDocument = objApp.ActiveDocument
    
    WinWordVBA = True
QuickExit:
End Function

No comments:

Post a Comment