Question : Excel Instances- Refering to the Instance in VBA

I need to open multiple instances of excel and be able to control each instance in VBA. I currently am storing the information for each instance when it is opened so that I can refer back to it. The problem with this approach is:

1. You have to open the instance using VBA in order to have a reference to it
2. It is unstable, any VBA error will erase the public variables where the instances are stored as "Microsoft Excel"
3. No way to store distinguishing instance information in a spreadsheet..because all instance variables are "Microsoft Excel"

I have found the following code that returns the handle for each excel instance running and stores it in an array. (This is a good start to being able to distinguish each instance and not have to deal with the generic "Microsoft Excel" reference. The two things I am looking for:

1. How do I take the handle information for each instance being stored in the array and use that in VBA to control that instance?

2. I need to be able to distinguish the instances so I know what handle refers to what instance. Perhaps through a workbook name in that instance or some other method so I know what instance of excel each handle is referring to.

My end goal:

Be able to record instance variables or handles into a spreadsheet with some unique identifiers so I can then reference that spreadsheet and pull the correct instance information to control the instance in VBA. I do not want to have to store everything in public variables or have to open the instance from VBA in order to have the ability to refer to it.



Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
Option Explicit 
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long 
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public hWndArray() As Long 

Private Const GW_HWNDNEXT = 2 
 
Sub xlInstances() 
  Dim hwnd As Long, lRet As Long
  'Dim hWndArray() As Long
  Dim i As Integer
  Dim sClassBuffer As String 
  i = 0
  hwnd = FindWindow("XLMAIN", vbNullString)
  If hwnd <> 0 Then
    ReDim hWndArray(i)
    hWndArray(i) = hwnd
    Do
      hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)
      If hwnd = 0 Then Exit Sub
      sClassBuffer = String(255, 0)
      lRet = GetClassName(hwnd, sClassBuffer, Len(sClassBuffer))
      sClassBuffer = Left(sClassBuffer, InStr(1, sClassBuffer, Chr(0), vbTextCompare) - 1)
      If UCase(sClassBuffer) = "XLMAIN" Then
        i = i + 1
        ReDim Preserve hWndArray(i)
        hWndArray(i) = hwnd
      End If
    Loop
  End If 
 
 
End Sub

Answer : Excel Instances- Refering to the Instance in VBA

Hi kyle972,
I don't think you can get the object reference from the windows handle. The GetObject function with the class argument "Excel.Application" does not return references to all instances. But, it might be possible to use the GetObject function with the pathname argument if you make sure that each running instance has an open workbook and you use the fullname of that workbook as the argument. It can be an empty workbook and it can be hidden.
Since the object returned by GetObject will be a workbook, you will of course have to use the application property like in this example.

Set myXlApp1 = GetObject("C:\wkb1.xls").Application

You can easily restore the references in your main application.
Could this solution be useful in your case? Please tell me if you need further assistance.

Kind regards,
Stellan
Random Solutions  
 
programming4us programming4us