#If VBA7 Then
Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "oleacc" ( _
ByVal hwnd As LongPtr, ByVal dwId As Long, riid As Any, ppvObject As Object) As Long
Private Declare PtrSafe Function FindWindowExA Lib "user32" ( _
ByVal hwndParent As LongPtr, ByVal hwndChildAfter As LongPtr, _
ByVal lpszClass As String, ByVal lpszWindow As String) As LongPtr
#Else
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" ( _
ByVal hwnd As Long, ByVal dwId As Long, riid As Any, ppvObject As Object) As Long
Private Declare Function FindWindowExA Lib "user32" ( _
ByVal hwndParent As Long, ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, ByVal lpszWindow As String) As Long
#End If
Sub Test()
Dim xl As Application, Wb
For Each xl In GetExcelInstances()
Debug.Print "Instance de: " & xl.ActiveWorkbook.FullName
For Each Wb In xl.Workbooks
Debug.Print "Classeur :", Wb.Name
If xl.ActiveWorkbook.FullName <> ActiveWorkbook.FullName Then
Wb.Activate
' xl.ActiveWorkbook.Worksheets(1).Range("A1").Select
' xl.ActiveWorkbook.Worksheets(1).Range("ZZ1").Select
' xl.ActiveWorkbook.Worksheets(1).Range("ZZ1").Value = "coucou de " & ActiveWorkbook.Name
End If
Next Wb
Debug.Print "============================="
Next
End Sub
Public Function GetExcelInstances() As Collection
Dim guid&(0 To 3), acc As Object, hwnd, hwnd2, hwnd3
guid(0) = &H20400: guid(1) = &H0: guid(2) = &HC0: guid(3) = &H46000000
Set GetExcelInstances = New Collection
Do
hwnd = FindWindowExA(0, hwnd, "XLMAIN", vbNullString)
If hwnd = 0 Then Exit Do
hwnd2 = FindWindowExA(hwnd, 0, "XLDESK", vbNullString)
hwnd3 = FindWindowExA(hwnd2, 0, "EXCEL7", vbNullString)
If AccessibleObjectFromWindow(hwnd3, &HFFFFFFF0, guid(0), acc) = 0 Then
On Error Resume Next
GetExcelInstances.Add acc.Application, CStr(acc.Application.hwnd) ' sans doublon
On Error GoTo 0
End If
Loop
End Function