Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetObject - how select what to get ?

Status
Not open for further replies.

jackha18

Technical User
Sep 28, 2002
39
0
0
IL
Micsrosoft says (in MSDN) when there is more than one instance of application is running there is no way know what instance GetObject() will return.

Is there is any other way I can cycle through all instances of the application and get a pointer to one of them I choose.
 
Hopefully this will help for further research. Your requirement would seem that a simplified version of this would work because your application is running and you will know the window name.
Code:
'======================================================
Private Declare Function M_GetActiveWindow Lib "user32" _
    Alias "GetActiveWindow" () As Long
'-
Private Declare Function FindWindow Lib "user32.dll" _
    Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
'-
Private Declare Function BringWindowToTop Lib "user32.dll" _
    (ByVal hWnd As Long) As Long
'------------------------------------------
Sub GET_WINDOW()
    Dim MyWindowName As String
    Dim xHwnd As Long
    '------------------
    Shell "C:\Calc.exe", 1
    MyWindowName = "Calculator"
    xHwnd = FindWindow(CLng(0), MyWindowName)
    retval = BringWindowToTop(xHwnd)
End Sub
'-------------------------------------------

Regards
BrianB
Use CupOfCoffee to speed up all windows applications.
It is easy until you know how.
================================
 
Sorry, but it seems like I din't explained the problem properly.

Suppose I have two instances of Winword.exe running and I want to use automation (e.g. from Excel) to work with one of them.

I call GetObject function and it gives me a pointer to one of the instances. The parameter passed to GetObject is window class rather than name so (and this is what MSDN says) I will get a pointer to one of the two randomly selected instances. What if I want to work with the other one? How can I select switch between the two?
 
Hi jackha18,

MSDN is right, of course!! So GetObject is not for you.

If you want to specify a particular instance of an app, you must have some information about it, so what do you know? And why can't you use FindWindow?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
If you want a particular instance of an app, just use CreateObject to have your own one.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Suppose I do know the Word window title. I then use FindWindow and get its handle. If I want to use automation with this window (this instance of application), how do I get a reference to that specific instance of application. hWnd only references to a window - it does not allow me to automate application.
 
Another option is to talk directly to some of the underlying COM tables. Have a look at GetRunningObjectTable which supplies a pointer to the IRunningObjectTable interface. This latter interface will allow you to enumerate all the running objects and then to return an 'object pointer' to any one of them that you so choose
 

Are you saying you have multiple instances of Word? This is unusual - just how are they being created? And why do you need a particular one?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top