LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
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.


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - New Instance JMay Excel Discussion (Misc queries) 4 November 28th 09 04:55 PM
Excel instance not shutting down Macca Excel Programming 0 December 13th 04 03:53 PM
Instance of Wizard excel add-in Yattabi Excel Programming 0 December 7th 04 03:37 PM
Excel Instance FuzzyLogic Excel Programming 2 February 21st 04 03:41 PM
Second instance of Excel Dom[_2_] Excel Programming 1 August 25th 03 05:37 PM


All times are GMT +1. The time now is 02:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"