Microsoft KB Archive/99955

{|
 * width="100%"|

Determining the Number of Instances an Application Is Running

 * }

Q99955

-

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

-

SUMMARY
Sometimes your WordBasic macro may need to determine how many instances of an application are currently running in memory.

Word 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, &quot;Active Application List&quot; Text 156, 14, 138, 13, &quot;Active Applications.&quot;, .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.x
In 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. &quot;MSWORD&quot; 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 &quot;EXCEL.&quot;)

Declare Function GetModuleHandle Lib &quot;Kernel&quot;(ModuleName$) As Integer Declare Function GetModuleUsage Lib &quot;Kernel&quot;(hTask As Integer) As Integer Sub MAIN Number = GetModuleUsage(GetModuleHandle(&quot;MSWORD&quot;)) If number = 1 Then MsgBox &quot;There is&quot; + Str$(Number) +\ &quot; instance of Word running.&quot; Else MsgBox &quot;There are&quot; + Str$(Number) +\ &quot; instances of Word running.&quot; EndIf End Sub 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 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.