View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default How to get all Excel instance

Not sure it can be done with GetObject, but this is how you can do it with
the Windows API:


Option Explicit
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Public Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4
Public Const GW_CHILD = 5

Sub test()

Dim i As Long
Dim collWindows As Collection

Set collWindows = New Collection

FindWindowHwndLike 0, "XLDESK", "", 0, 0, collWindows

If collWindows.Count 0 Then
For i = 1 To collWindows.Count
MsgBox collWindows(i)
Next
End If

End Sub

Function FindWindowHwndLike(hWndStart As Long, _
ClassName As String, _
WindowTitle As String, _
level As Long, _
lHolder As Long, _
collWindows As Collection) As Long

Dim hwnd As Long
Dim sWindowTitle As String
Dim sClassName As String
Dim r As Long

If level = 0 Then
If hWndStart = 0 Then
hWndStart = GetDesktopWindow()
End If
End If

'Increase recursion counter
'--------------------------
level = level + 1

'Get first child window
'----------------------
hwnd = GetWindow(hWndStart, GW_CHILD)

Do While hwnd 0
'Search children by recursion
'----------------------------
lHolder = FindWindowHwndLike(hwnd, _
ClassName, _
WindowTitle, _
level, _
lHolder, _
collWindows)

'Get the window text
'-------------------
sWindowTitle = Space$(255)
r = GetWindowText(hwnd, sWindowTitle, 255)
sWindowTitle = Left$(sWindowTitle, r)

'get the class name
'------------------
sClassName = Space$(255)
r = GetClassName(hwnd, sClassName, 255)
sClassName = Left$(sClassName, r)

If (InStr(1, sWindowTitle, WindowTitle, vbBinaryCompare) 0 Or _
sWindowTitle = WindowTitle) And _
(sClassName Like ClassName & "*" Or _
sClassName = ClassName) Then
FindWindowHwndLike = hwnd
lHolder = hwnd
collWindows.Add hwnd
End If

'Get next child window
'---------------------
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop

FindWindowHwndLike = lHolder

End Function


RBS


"Xav" wrote in message
oups.com...
For my application I would like to retrieve all Excel instances
runnning on a specific machine. Basically I want to use the GetObject
method and stock it in an array or whatsoever. Does anybody have an
idea?
Thanks for helping.