ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA to return how many Excel instances are open (https://www.excelbanter.com/excel-programming/329093-vba-return-how-many-excel-instances-open.html)

tod

VBA to return how many Excel instances are open
 
Could someone help me with code that will tell me how many instances of
Excel are currently open?


Stephen Bullen[_4_]

VBA to return how many Excel instances are open
 
Hi Tod,

Could someone help me with code that will tell me how many instances of
Excel are currently open?


Sure (careful with the word-wrap):

Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal
hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Function ExcelInstances() As Long

Dim hWndDesk As Long
Dim hWndXL As Long

'Get a handle to the desktop
hWndDesk = GetDesktopWindow

Do
'Get the next Excel window
hWndXL = FindWindowEx(GetDesktopWindow, hWndXL, "XLMAIN", vbNullString)

'If we got one, increment the count
If hWndXL 0 Then
ExcelInstances = ExcelInstances + 1
End If

'Loop until we've found them all
Loop Until hWndXL = 0


End Function


Regards

Stephen Bullen
Microsoft MVP - Excel

Professional Excel Development
The most advanced Excel VBA book available
www.oaltd.co.uk/ProExcelDev




All times are GMT +1. The time now is 11:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com