WD:Macro to Determine If Application Is Running

Last reviewed: February 2, 1998
Article ID: Q94973

The information in this article applies to:
  • Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Word for the Macintosh, versions 6.0, 6.0.1

SUMMARY

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.

MORE INFORMATION

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.

Word for Windows

   Sub MAIN
      If AppIsRunning("File Manager") Then
         AppActivate "File Manager"
      Else
         Shell "WINFILE.EXE"
      End If
   End Sub

Word for the Macintosh

   Sub Main
      If AppIsRunning(MacID$("ttxt")) Then
         AppActivate MacID$("ttxt")
      Else
         Shell MacID$("ttxt")
      End If
   End Sub

In Word version 2.x for Windows you can call GetModuleHandle in the Windows Kernel dynamic-link library (DLL) to determine if an application is running. The following Word 2.x macro example demonstrates the use of the Windows GetModuleHandle function to determine if Microsoft Excel is already running.

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

The GetModuleHandle function uses the module name for the application to determine if an application is running. The module name for an application is usually the name of the executable file.

   Module Name       Program .EXE Filename
   -----------       ---------------------

   Excel             EXCEL.EXE

   Msmail            MSMAIL.EXE

   Msaccess          MSACCESS.EXE

IsAppLoaded is an alias for the GetModuleHandle function. Any name can be used in place of the IsAppLoaded alias used in the above macro Declare statement.

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

The following Word 2.x macro example uses the Windows GetModuleHandle function to determine if Microsoft Excel is already running. If Excel is already running, Word initiates a DDE conversation with Excel in order to maximize the Excel application window.

   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

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.


Additional query words: winword2 GetModuleHandle winword word6 running
loaded macword isapploaded declare dll dde
Keywords : kbmacroexample kbmacro
Version : WINDOWS:2.x,6.0,6.0a,6.0c; MACINTOSH:6.0,6.0.1
Platform : MACINTOSH WINDOWS
Issue type : kbhowto kbinfo
Solution Type : kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.