Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public hWndArray() As Long
Private Const GW_HWNDNEXT = 2
Sub xlInstances()
Dim hwnd As Long, lRet As Long
'Dim hWndArray() As Long
Dim i As Integer
Dim sClassBuffer As String
i = 0
hwnd = FindWindow("XLMAIN", vbNullString)
If hwnd <> 0 Then
ReDim hWndArray(i)
hWndArray(i) = hwnd
Do
hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)
If hwnd = 0 Then Exit Sub
sClassBuffer = String(255, 0)
lRet = GetClassName(hwnd, sClassBuffer, Len(sClassBuffer))
sClassBuffer = Left(sClassBuffer, InStr(1, sClassBuffer, Chr(0), vbTextCompare) - 1)
If UCase(sClassBuffer) = "XLMAIN" Then
i = i + 1
ReDim Preserve hWndArray(i)
hWndArray(i) = hwnd
End If
Loop
End If
End Sub
|