+ Reply to Thread
Results 1 to 12 of 12

Multiple Instances of Excel

  1. #1
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Multiple Instances of Excel

    First time poster of this site.... be gentle

    I am trying to work in multiple instances of excel, but am having no luck in using vb code in my activated instance. This is my code:
    PHP Code: 
    For Each process In GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_process ")
            If 
    process.Name "EXCEL.EXE" Then
                
    'MsgBox process.Handle
                count = 0
                hand = process.Handle
                AppActivate (hand)
                '
    SendKeys ("{DOWN}"), True
                
    For Each wkb In Workbooks
                    
    If Windows(wkb.Name).Visible Then
                       book
    (count) = wkb.Name
                    End 
    If
                    
    count count 1
                Next
            End 
    If
        
    Next
    MsgBox book
    (0) & book(1) & book(2) & book(3) & book(4) & book(5) & book(6)

    End Sub 
    I can send keystrokes to the activated app, but I can't seem to perform any coded functions on it. Instead it just performs the code on the instance that the code resides in.

    Any help would be great..

    Thanks.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Multiple Instances of Excel

    Hello maru487,

    Are you trying to control separate instances from your workbook? If so then you will need to use the Windows API to attach to the thread of instance of interest. Let me know.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Multiple Instances of Excel

    yes, that's exactly what i want to do. control another workbook in another instance of excel.

    Unfortunately, I can't control if the user creates a new instance, so I need to be able to operate on any spreadsheet that is open in Windows.

    Could you give an example of the API in action?

    thanks in advance

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Multiple Instances of Excel

    Hello maru487,

    Here is a macro I wrote last year to activate other instances of Excel. I will work on paring this down for what you need to do. The WMI can return the PID for each instance which makes recoding this easier.

    Activate a Separate Instance of Excel
    Please Login or Register  to view this content.

    Example
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Multiple Instances of Excel

    I'm only a beginner programmer, but it looks as though the routine requires knowing the name of the workbook in the other instance? I guess I didn't say that, I also won't know the name of the workbooks.

    When the userform initializes, my hope is to retrieve all of the workbook names from all of the instances of Excel and put them in a combobox. The user then chooses which file they want to perform the script on.

    Is there a way to retrieve the filenames of all the workbooks in all of the instances? Or is that impossible?

    Thanks again....

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Multiple Instances of Excel

    Hello maru487,

    Once you have the program ID (pid) of the instance and its window handle (hWnd) you can do just about anything. So, yes all the workbooks and their worksheets can be manipulated. Glad you told me this before I started to code the macro for you.

  7. #7
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Multiple Instances of Excel

    I am out of it... you're using the workbook_name to compare, so obviously you are able to obtain the filename of any workbook... duh!

    Unless you are wanting to create some code out of interest sake, don't worry about creating a routine for me. I'm going to give it a go with the info here.

    I'll post what I come up with.


    Thanks a million... I'll pay it forward

  8. #8
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Multiple Instances of Excel

    I'm a little puzzled as to why the string and IIF functions are necessary?

    PHP Code: 
    WindowTitle String(512Chr$(0))
                  
    GetWindowText(hWndWindowTitle512)
                  
    WindowTitle IIf(0Left(WindowTitleL), ""
    Wouldn't this do?

    PHP Code: 

    Private Declare Function GetWindowText _
      Lib 
    "User32.dll" _
        Alias 
    "GetWindowTextA" _
          
    (ByVal hWnd As Long) As Long
    .
    .
    .
    WindowTitle GetWindowText(hWnd

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Multiple Instances of Excel

    Hello maru487,

    Application Interface Programming (API) calls often use buffers to hold text information. The entire API declaration is below:
    Please Login or Register  to view this content.
    1. The variable hWnd (window handle) is assigned by the system to each window that is created. This uniquely identifies the window of interest.
    2. The variable lpString is a long pointer to the first word in the BString in memory that will be used to hold the name of the window.
    3. The last variable nMaxCount holds the maximum byte count for the buffer pointed to by lpString.

    To use this call correctly and not crash your machine, you must set up a buffer according to the API call requirements. The code below creates a string buffer that can holds 512 characters,
    Please Login or Register  to view this content.

    The return variable L will hold the byte count, if the call was successful.
    Please Login or Register  to view this content.
    The immediate IF statement evaluates the API call's success. If successful the window title is equal to the number of bytes, L, in the buffer. If not then a window title is set to a VBA empty string.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Multiple Instances of Excel

    Hello maru487,

    Application Interface Programming (API) calls often use buffers to hold text information. The entire API declaration is below:
    Please Login or Register  to view this content.
    1. The variable hWnd (window handle) is assigned by the system to each window that is created. This uniquely identifies the window of interest.
    2. The variable lpString is a long pointer to the first word in the BString in memory that holds the name of the window.
    3. The last variable nMaxCount holds the maximum byte count for the buffer pointed to by lpString.

    To use this call correctly and not crash your machine, you must set up a buffer according to the API call requirements. The code below creates a string buffer that can holds 512 characters,
    Please Login or Register  to view this content.

    The return variable L will hold the byte count, if the call was successful.
    Please Login or Register  to view this content.

    The immediate IF statement evaluates the API call's success. If successful the window title is equal to the number of bytes, L, in the buffer. If not then the window title is set to a VBA empty string.
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Multiple Instances of Excel

    BTW thank you for the info about string, etc. it's basically a translation thing between VB and API... I think?

    I tried to activate a workbook and test it out with your code, exactly as pasted here, but when I try to perform a task on the Activated workbook, it still operates on the workbook that the VBA code is in

    PHP Code: 
    Private Sub CommandButton5_Click()
    ActivateWorkbook "book2"
    Range("A1").Value 4
    End Sub 
    Is there a with activeworkbook needed? I've tried all different avenues and permutations, but I seem to be spinning my wheels.

    Thanks in advance and thanks again.... all this info is very helpful.

  12. #12
    Registered User
    Join Date
    10-29-2010
    Location
    bc, canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Multiple Instances of Excel

    bump....bump

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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