Determining the Number of Instances an Application Is RunningLast reviewed: July 30, 1997Article ID: Q99955 |
The information in this article applies to:
SUMMARYSometimes your WordBasic macro may need to determine how many instances of an application are currently running in memory.
MORE INFORMATIONWord 6.0 In Word 6.0, API calls may be avoided by displaying the active applications alphabetically in a listbox similar to the Task List. This sample macro may be enhanced to return the actual number of a specific window if that is desired.
Sub MAIN size = AppCount() - 1 Dim winnames$(size) AppGetNames winnames$() SortArray winnames$() Begin Dialog UserDialog 420, 162, "Active Application List" Text 156, 14, 138, 13, "Active Applications.", .Text1 ListBox 41, 35, 340, 84, winnames$(), .ListBox1 OKButton 170, 133, 88, 21 End Dialog Dim dlg As UserDialog Dialog dlg End Sub Word 2.xIn Word for Windows 2.x, you can call GetModuleUsage in the Windows Kernel dynamic-link library (DLL) to determine how many instances of an application are running in memory. The following macro example demonstrates the use of the Windows GetModuleUsage function to determine how many instances of Word for Windows are running. "MSWORD" is the module name for Word for Windows, but any other application module name can be substituted below. (For example, the module name for Microsoft Excel is "EXCEL.")
Declare Function GetModuleHandle Lib "Kernel"(ModuleName$)As Integer Declare Function GetModuleUsage Lib "Kernel"(hTask AsInteger) As Integer
Sub MAINNumber = GetModuleUsage(GetModuleHandle("MSWORD")) If number = 1 Then MsgBox "There is" + Str$(Number) +\ " instance of Word running."Else MsgBox "There are" + Str$(Number) +\ " instances of Word running."EndIf End SubThe 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.EXEWARNING: 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.
REFERENCES"Using WordBasic," by WexTech Systems and Microsoft, pages 30-44
|
KBCategory: kbmacro
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |