WD:Macro to Determine If Application Is RunningLast reviewed: February 2, 1998Article ID: Q94973 |
The information in this article applies to:
SUMMARYIn 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 INFORMATIONWord 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 SubIn 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 SubThe 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.EXEIsAppLoaded 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 SubThe 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 SubWARNING: 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |