ID: Q94973
In Word, you may need to determine if a program is running. When you write WordBasic macros that communicate with other applications (through dynamic data exchange [DDE]), this determination is often necessary because prior to starting a DDE conversation using the WordBasic DDEInitiate command, the application you intend to communicate with must be running.
Word 6.0 includes an AppIsRunning() function that can be used to determine if an application is already running. The AppIsRunning() function returns -1 if the specified application is running or returns 0 (zero) if it is not.
For example, the following Word 6.0 sample macro determines if File Manager is running. If the application is running, File Manager is activated, and if not, WINFILE.EXE (File Manager) is started.
   Sub MAIN
      If AppIsRunning("File Manager") Then
         AppActivate "File Manager"
      Else
         Shell "WINFILE.EXE"
      End If
   End Sub
   Sub Main
      If AppIsRunning(MacID$("ttxt")) Then
         AppActivate MacID$("ttxt")
      Else
         Shell MacID$("ttxt")
      End If
   End Sub
This macro checks to see if Microsoft Excel is running. The macro uses AppActivate to switch the focus to the Microsoft Excel application. If the application is not running, the Shell statement runs EXCEL.EXE. If the EXCEL directory is not in your MS-DOS path, you can indicate a full path to the Microsoft Excel executable file (Shell "C:\Excel\Excel.exe").
   Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias \ 
   "GetModuleHandle"
   Sub Main
      If IsAppLoaded("EXCEL") = 0 Then
         Shell "Excel.exe"
      Else
         AppActivate "Microsoft Excel"
      End If
   End Sub
   Module Name       Program .EXE Filename
   -----------       ---------------------
   Excel             EXCEL.EXE
   Msmail            MSMAIL.EXE
   Msaccess          MSACCESS.EXE
The following macro example demonstrates the use of the Windows GetModuleHandle function to determine if Microsoft Access is already running.
   Declare Function IsAppLoaded Lib "kernel"(name$) As Integer Alias \ 
   "GetModuleHandle"
   Sub Main
      If IsAppLoaded("msaccess") = 0 Then
         Shell "msaccess.exe"
      Else
         AppActivate "Microsoft Access"
      End If
   End Sub
   Declare Function isapploaded Lib "KERNEL"(name$) As Integer Alias \ 
   "GetModuleHandle"
   Sub MAIN
      If IsAppLoaded("Excel") = 0 Then
         Shell "Excel.exe", 3 'Microsoft Excel must be on the MS-DOS path
      Else
         ChanNum1 = DDEInitiate("Excel", "system")
         DDEExecute ChanNum1, "[APP.MAXIMIZE()]"
         DDETerminate ChanNum1
      End If
   End Sub
Additional query words: winword2 GetModuleHandle winword word6 running loaded macword isapploaded declare dll dde
Keywords          : kbmacro kbmacroexample 
Version           : WINDOWS:2.x,6.0,6.0a,6.0c; MACINTOSH:6.0,6.0.1
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto kbinfoLast Reviewed: February 3, 1998