+ Reply to Thread
Results 1 to 5 of 5

Visible Instances of an Application

  1. #1
    TCook
    Guest

    Visible Instances of an Application

    Hello All,

    I am building something that needs to grab a certain instance of MS Excel.
    Unfortunately, "GetObject" randomly chooses an instance and, therefore, is
    not appropriate / robust enough by itself. In pseudo code, what I need is
    below:

    For each pProcess in Processes
    If instr(pProcess.Name, "Excel") > 0 And pProcess.Visible = True
    Then
    Set MyAppInstance = pProcess.Instance

    Exit For
    End If
    Next pProcess

    Thanks & Regards,

    TC



  2. #2
    William DePalo [MVP VC++]
    Guest

    Re: Visible Instances of an Application

    "TCook" <[email protected]> wrote in message
    news:elz%[email protected]...
    > I am building something that needs to grab a certain instance of MS Excel.
    > Unfortunately, "GetObject" randomly chooses an instance and, therefore, is
    > not appropriate / robust enough by itself. In pseudo code, what I need is
    > below:
    >
    > For each pProcess in Processes
    > If instr(pProcess.Name, "Excel") > 0 And pProcess.Visible = True
    > Then
    > Set MyAppInstance = pProcess.Instance
    >
    > Exit For
    > End If
    > Next pProcess


    This isn't really a kernel issue but as you posted here, and as you seem to
    be after a process list, CreateToolhelp32Snapshot(), Process32First(), and
    Process32Next() will get that for you. You can check the docs for the
    ProcessEntry32 structure to see if it contains the data that you need.

    Just by the way, the main window for the version of Excel I have installed
    here has a window class name of "XLMAIN". If the class name is constant
    across versions you could use EnumWindows() to list the windows and
    GetClassName() to filter out Excel's.

    Regards,
    Will



  3. #3
    TCook
    Guest

    Re: Visible Instances of an Application

    Hey Will,

    Thanks for the info. After obtaining the appropriate process, can I then
    actually get the instance of the Excel app such that I can work with its
    object model?

    Regards,

    Todd



    "William DePalo [MVP VC++]" <[email protected]> wrote in message
    news:[email protected]...
    > "TCook" <[email protected]> wrote in message
    > news:elz%[email protected]...
    >> I am building something that needs to grab a certain instance of MS
    >> Excel. Unfortunately, "GetObject" randomly chooses an instance and,
    >> therefore, is not appropriate / robust enough by itself. In pseudo code,
    >> what I need is below:
    >>
    >> For each pProcess in Processes
    >> If instr(pProcess.Name, "Excel") > 0 And pProcess.Visible = True
    >> Then
    >> Set MyAppInstance = pProcess.Instance
    >>
    >> Exit For
    >> End If
    >> Next pProcess

    >
    > This isn't really a kernel issue but as you posted here, and as you seem
    > to be after a process list, CreateToolhelp32Snapshot(), Process32First(),
    > and Process32Next() will get that for you. You can check the docs for the
    > ProcessEntry32 structure to see if it contains the data that you need.
    >
    > Just by the way, the main window for the version of Excel I have installed
    > here has a window class name of "XLMAIN". If the class name is constant
    > across versions you could use EnumWindows() to list the windows and
    > GetClassName() to filter out Excel's.
    >
    > Regards,
    > Will
    >




  4. #4
    Steve Yandl
    Guest

    Re: Visible Instances of an Application

    Todd,

    The MS Word object has a 'Tasks' collection that will let you isolate
    visible instances of active Windows. It might be worth the trouble to
    create a hidden instance of Word and get the list.

    Steve


    "TCook" <[email protected]> wrote in message
    news:[email protected]...
    > Hey Will,
    >
    > Thanks for the info. After obtaining the appropriate process, can I then
    > actually get the instance of the Excel app such that I can work with its
    > object model?
    >
    > Regards,
    >
    > Todd
    >
    >
    >
    > "William DePalo [MVP VC++]" <[email protected]> wrote in message
    > news:[email protected]...
    >> "TCook" <[email protected]> wrote in message
    >> news:elz%[email protected]...
    >>> I am building something that needs to grab a certain instance of MS
    >>> Excel. Unfortunately, "GetObject" randomly chooses an instance and,
    >>> therefore, is not appropriate / robust enough by itself. In pseudo
    >>> code, what I need is below:
    >>>
    >>> For each pProcess in Processes
    >>> If instr(pProcess.Name, "Excel") > 0 And pProcess.Visible = True
    >>> Then
    >>> Set MyAppInstance = pProcess.Instance
    >>>
    >>> Exit For
    >>> End If
    >>> Next pProcess

    >>
    >> This isn't really a kernel issue but as you posted here, and as you seem
    >> to be after a process list, CreateToolhelp32Snapshot(), Process32First(),
    >> and Process32Next() will get that for you. You can check the docs for the
    >> ProcessEntry32 structure to see if it contains the data that you need.
    >>
    >> Just by the way, the main window for the version of Excel I have
    >> installed here has a window class name of "XLMAIN". If the class name is
    >> constant across versions you could use EnumWindows() to list the windows
    >> and GetClassName() to filter out Excel's.
    >>
    >> Regards,
    >> Will
    >>

    >
    >




  5. #5
    Someone
    Guest

    Re: Visible Instances of an Application

    If you are doing this for the purpose of automation, then perhaps you may
    want to explain why you need this further as others may have alternative
    solutions. For example, if you were referencing an instance and then
    released it, then you have to keep it for as long as needed.

    Also, what Office versions are you targeting? Generally the higher the
    version, the easier to work with.

    Making a routine that gets references to all open Excel instances involves
    making dummy Add-ins, DLL's, and interprocess communications. Not a simple
    process. I have not done it before with Office applications.

    In Excel XP or after, you can use ActiveX EXE's as Add-ins, which might
    simplify the process.

    The add-in DLL/EXE would get Excel's instance and deliver it to your EXE.
    The problem is that you can't use ObjPtr(), VarPtr(), CopyMemory and send
    the reference to Application or any other object to another process(EXE), if
    you do, the application will crash. The only way to do it is through DCOM,
    see the KB articles below which return a recordset object, you can use the
    same method to return Excel.Application. You may have to do something like
    Coffee2 sample that comes with MSDN, which is not very simple.

    Links:

    How To Create a DCOM Client/Server Application by Using Visual Basic
    http://support.microsoft.com/default...b;en-us;266717

    How To Create a DCOM Client/Server with Events by Using Visual Basic
    http://support.microsoft.com/default...b;en-us;267836

    Using DCOM with complex objects:

    How To Implement Visual Basic COM Objects Returning Recordsets
    http://support.microsoft.com/default...b;en-us;224424

    INFO: Passing ADO Recordsets in Visual Basic Procedures
    http://support.microsoft.com/default...b;en-us;193871

    Coffee2 sample:

    http://msdn.microsoft.com/library/de...ffeeCoffee.asp
    http://msdn.microsoft.com/library/de...entsinking.asp



    "TCook" <[email protected]> wrote in message
    news:elz%[email protected]...
    > Hello All,
    >
    > I am building something that needs to grab a certain instance of MS Excel.
    > Unfortunately, "GetObject" randomly chooses an instance and, therefore, is
    > not appropriate / robust enough by itself. In pseudo code, what I need is
    > below:
    >
    > For each pProcess in Processes
    > If instr(pProcess.Name, "Excel") > 0 And pProcess.Visible = True
    > Then
    > Set MyAppInstance = pProcess.Instance
    >
    > Exit For
    > End If
    > Next pProcess
    >
    > Thanks & Regards,
    >
    > TC
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1