Option Explicit
Private Type GUID
lData1 As Long
iData2 As Integer
iData3 As Integer
aBData4(0 To 7) As Byte
End Type
Private 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
Private Declare Sub AccessibleObjectFromWindow Lib "OLEACC.DLL" _
(ByVal hwnd As Long, _
ByVal dwId As Long, _
riid As GUID, _
ppvObject As Any)
Private Const OBJID_NATIVEOM = &HFFFFFFF0
Private Sub SetIDispatch(ByRef ID As GUID)
' IDispatch Interface.
' {00020400-0000-0000-C000-000000000046}.
With ID
.lData1 = &H20400
.iData2 = &H0
.iData3 = &H0
.aBData4(0) = &HC0
.aBData4(1) = &H0
.aBData4(2) = &H0
.aBData4(3) = &H0
.aBData4(4) = &H0
.aBData4(5) = &H0
.aBData4(6) = &H0
.aBData4(7) = &H46
End With
End Sub
Private Sub Get_XL_APP_Collection(ByRef Col As Collection)
Dim IDispatch As GUID
Dim oWB As Object
Dim lXLhwnd As Long
Dim lXLDESKhwnd As Long
Dim lWBhwnd As Long
Do
lXLhwnd = FindWindowEx(0, lXLhwnd, "XLMAIN", vbNullString)
If lXLhwnd = 0 Then
Exit Do
Else
lXLDESKhwnd = FindWindowEx(lXLhwnd, 0&, "XLDESK", vbNullString)
lWBhwnd = FindWindowEx(lXLDESKhwnd, 0&, "EXCEL7", vbNullString)
If lWBhwnd Then
SetIDispatch IDispatch
Call AccessibleObjectFromWindow _
(lWBhwnd, OBJID_NATIVEOM, IDispatch, oWB)
Col.Add oWB.Application
End If
End If
Loop
Set oWB = Nothing
End Sub
Sub test()
Dim XL_Col As New Collection
Dim i As Integer
Dim j As Integer
Get_XL_APP_Collection XL_Col
For i = 1 To XL_Col.Count
For j = 1 To XL_Col.Item(i).Workbooks.Count
ActiveSheet.ListBox1.AddItem "Excel Instance : (" & i & ") " & _
" Workbook : (" & XL_Col.Item(i).Workbooks(j).Name & ")"
Next j
Next i
End Sub