ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to get all Excel instance (https://www.excelbanter.com/excel-programming/348892-how-get-all-excel-instance.html)

Xav[_2_]

How to get all Excel instance
 
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.


Peter T

How to get all Excel instance
 
If you know the FullName of a file that's uniquely open in an instance then
you can use

On error resume next
set xlApp = GetObject(strFullName).Parent
If not xlApp is nothing then

So if you are concerned with various known files that may be open in
different instances (not open multiple as read-only) you can set up an array
or collection of WithEvents excel.application type class's.

If you don't know a unique file in given instances it's significantly more
complicated.

Regards,
Peter T


"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.




RB Smissaert

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.




All times are GMT +1. The time now is 12:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com