Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting the number of windows 1

Status
Not open for further replies.

Molby

Technical User
May 15, 2003
520
GB
Is it possible to count the number of Windows open on a pc using Excel VBA? I.e. if I had open Excel, Outlook, Access, IE Explorer, it would return 4.
 
I found this link which you may find of some use:

If I get time I will try to post a better solution.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks Clive I think that is what I'm after, I'll get back to you.
 
It might be worth investigating the Win API EnumWindows function. You could try asking in the Win API forum or the VB Win32API forum.



Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
The EnumWindows API will return ALL open windows, including all of the children. With just Excel, Access, IE, and Outlook open, you will get considerable more than 4 windows back. Not to mention the windows of those processes running in the SystemTray, or in the background that have hidden windows.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Yeah, when I run this code, I end up with about 25 entries as Cajun suggested. Any other ideas?
 
Is the question that you want answered, "How many Applications are running?"

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Cajun, Yes, the number of apps running would be perfect.
 
Sorry it took so long, but work got in the way. The following code will build a collection containing the names of the processes currently running on the machine. Please keep in mind that some of these are running in the System Tray, and some are running in the background.

In a separate standard code module, or in the declarations sections of your form, place the following API declarations, type structure, and constant definitions:
Code:
Public Const TH32CS_SNAPHEAPLIST = &H1
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPTHREAD = &H4
Public Const TH32CS_SNAPMODULE = &H8
Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Public Const TH32CS_INHERIT = &H80000000
Public Const MAX_PATH As Integer = 260

Public Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * MAX_PATH
End Type

Public Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Then, at the appropriate location in your code, I've used a command button on a form, drop in the following code:
Code:
Private Sub cmdTestIt_Click()
    
   Dim lLng_hSnapShot      As Long
   Dim lPrc_ProcessInfo    As PROCESSENTRY32
   Dim lLng_RetVal         As Long
   Dim lCol_ExeRunning     As New Collection
   Dim lStr_ExeName        As String
   Dim lInt_NullLoc        As Integer
   
   lLng_hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
   lPrc_ProcessInfo.dwSize = Len(lPrc_ProcessInfo)
   lLng_RetVal = Process32First(lLng_hSnapShot, lPrc_ProcessInfo)
   Do While (lLng_RetVal > 0)
      lInt_NullLoc = InStr(lPrc_ProcessInfo.szExeFile, Chr(0))
      If (lInt_NullLoc > 0) Then
         lStr_ExeName = Left(lPrc_ProcessInfo.szExeFile, lInt_NullLoc)
      Else
         lStr_ExeName = Trim(lPrc_ProcessInfo.szExeFile)
      End If
      lCol_ExeRunning.Add UCase(lStr_ExeName)
      lLng_RetVal = Process32Next(lLng_hSnapShot, lPrc_ProcessInfo)
   Loop
   CloseHandle lLng_hSnapShot

End Sub
When you close the handle, the collection will have all of the running processes.


Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I'm sorry, but there is one minor error. Please change the following line:

lStr_ExeName = Left(lPrc_ProcessInfo.szExeFile, lInt_NullLoc - 1)

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks Cajun, I can see the loop calling all the different apps, but which variable is counting them, or do I need a separate variable for that?
 
The .Count property of the collection would probably come in handy here.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks again, I know how to use .Count, I'm just unsure what variable to assign it to....
 
You can create your own variable for it. You can turn this into a function and return the .Count, you can assign to a global. You've got lots of different options.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Ah, I've got it now, thanks for your help, and have a star.
 
Thank you, and I'm glad you've got it working.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top