Microsoft KB Archive/94973

{|
 * width="100%"|

WD: Macro to Determine If Application Is Running

 * }

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
 * Microsoft 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(&quot;File Manager&quot;) Then AppActivate &quot;File Manager&quot; Else Shell &quot;WINFILE.EXE&quot; End If  End Sub

Word for the Macintosh
  Sub Main If AppIsRunning(MacID$(&quot;ttxt&quot;)) Then AppActivate MacID$(&quot;ttxt&quot;) Else Shell MacID$(&quot;ttxt&quot;) 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 &quot;C:\Excel\Excel.exe&quot;).

  Declare Function IsAppLoaded Lib &quot;kernel&quot;(name$) As Integer Alias \ &quot;GetModuleHandle&quot;

Sub Main If IsAppLoaded(&quot;EXCEL&quot;) = 0 Then Shell &quot;Excel.exe&quot; Else AppActivate &quot;Microsoft Excel&quot; 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 &quot;kernel&quot;(name$) As Integer Alias \ &quot;GetModuleHandle&quot;

Sub Main If IsAppLoaded(&quot;msaccess&quot;) = 0 Then Shell &quot;msaccess.exe&quot; Else AppActivate &quot;Microsoft Access&quot; 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 &quot;KERNEL&quot;(name$) As Integer Alias \ &quot;GetModuleHandle&quot;

Sub MAIN If IsAppLoaded(&quot;Excel&quot;) = 0 Then Shell &quot;Excel.exe&quot;, 3 'Microsoft Excel must be on the MS-DOS path Else ChanNum1 = DDEInitiate(&quot;Excel&quot;, &quot;system&quot;) DDEExecute ChanNum1, &quot;[APP.MAXIMIZE]&quot; 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 &quot;as is&quot; 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 : kbmacro kbmacroexample

Issue type : kbhowto kbinfo

Technology :