Hello everyone. First time posting here! Thanks in advance for your help.
A colleague of mine uses a tool from a company called MicroHedge (the tool is called Valdi Risk Analytics), and queries data from this tool from an instance of Excel.
I am assisting him in trying to find out how to access several instances of MicroHedge, and aggregate data through the various instances in one of several instances of Excel.
I'm trying out some code in VBA to help connect to this tool. After installing the program from MicroHedge, and setting the appropriate VBA references, the code below allows me to access the first instance of Microhedge through VBA.:
However, I would like to iterate through each open instance of MicroHedge. Using the GetObject(...) method appears to only choose the first instance.
The generic question for my issue is, how would one iterate through a list of COM objects, select the appropriate object, then assign a variable of a specific class to access the object?
I can iterate through the different COM objects open, and find multiple instances of MicroHedge, which are indicated by the different colItems with name "MH4.exe" using the following code:
This code breaks at the line:
where I get an error of "Run-time error '13': Type mismatch".
I hope I am only off by setting mhApp correctly. However, I'd surely appreciate any help in getting this to work correctly.
A colleague of mine uses a tool from a company called MicroHedge (the tool is called Valdi Risk Analytics), and queries data from this tool from an instance of Excel.
I am assisting him in trying to find out how to access several instances of MicroHedge, and aggregate data through the various instances in one of several instances of Excel.
I'm trying out some code in VBA to help connect to this tool. After installing the program from MicroHedge, and setting the appropriate VBA references, the code below allows me to access the first instance of Microhedge through VBA.:
Code:
Sub MicroHedgeTest()
Dim mhPos As MHPosition
Dim month As MHMonth
Dim mhApp As MHLib.MicroHedge
Set mhApp = GetObject(, "microhedge.application")
Set Book = mhApp.GetBook("")
If Book.Count > 0 Then
'Do something based on the book.
End If
However, I would like to iterate through each open instance of MicroHedge. Using the GetObject(...) method appears to only choose the first instance.
The generic question for my issue is, how would one iterate through a list of COM objects, select the appropriate object, then assign a variable of a specific class to access the object?
I can iterate through the different COM objects open, and find multiple instances of MicroHedge, which are indicated by the different colItems with name "MH4.exe" using the following code:
Code:
Dim mhPos As MHPosition
Dim month As MHMonth
Dim mhApp As MHLib.MicroHedge
Dim strComputer As String
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", , 48)
For Each objItem In colItems
If objItem.Name = "MH4.exe" Then
Set mhApp = objItem
Set Book = mhApp.GetBook("")
If Book.Count > 0 Then
'Do something based on the book.
End If
End If
Next
This code breaks at the line:
Code:
Set mhApp = objItem
I hope I am only off by setting mhApp correctly. However, I'd surely appreciate any help in getting this to work correctly.