Determine if an Application is Running

Sometimes your VBA applications have to communicate with another Windows application. In those situations it is useful to know if the other application is running prior to passing data or otherwise attempting to communicate with it. The easiest way to do this is to use the VBA GetObject function. You can easily wrap this in your own function as shown in the IsAppRunning function below. The appName parameter can be obtained from the Windows Registry under the HKEY_CLASSES_ROOT section. Some examples are shown below.

Public Function IsAppRunning(appName As String) As Boolean
    Dim objApp As Object
    On Error GoTo NOT_RUNNING

    Set objApp = GetObject(, appName)
    IsAppRunning = Not (objApp Is Nothing)
    Set objApp = Nothing
    Exit Function

NOT_RUNNING:
    IsAppRunning = False
    Set objApp = Nothing
End Function

Const APP_NAME_EXCEL = "Excel.Application"
Const APP_NAME_WORD = "Word.Application"
Const APP_NAME_AUTOCAD = "AutoCAD.Application"
Const APP_NAME_AUTOCAD_R18 = "AutoCAD.Application.18"
Const APP_NAME_AUTOCAD_R16 = "AutoCAD.Application.16"
Const APP_NAME_MICROSTATION_V7 = "MicroStation.Application"
Const APP_NAME_MICROSTATION_V8 = "MicroStationDGN.Application"

Sub TestExcel()
    MsgBox "Microsoft Excel is running = " _
& CStr(IsAppRunning(APP_NAME_EXCEL))
End Sub

EnvisionCAD

Since 1996, EnvisionCAD has been a nationally recognized leader in the configuration, customization, implementation, training & support for CAD software solutions. Our individualized approach has benefited private engineering firms and government agencies alike. Basic or advanced, we can help you get the most from your CAD technology.

Tags:

Leave a Reply

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