+ Reply to Thread
Results 1 to 12 of 12

Simple Example to Explain Defining all Open Workbooks

  1. #1
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Simple Example to Explain Defining all Open Workbooks

    Assume:

    1. Launch Excel. File > Open "Test.xlsm". From Excel, File > Open "Test2.xlsm". From Excel, File Open "Test3.xlsm"
    2. Start > Launch new instance of excel. File > Open "Test4.xlsm", File > Open "Test5.xlsm".

    1 instance of excel with 3 "open" workbooks, another separate instance of excel with two "open" workbooks".

    How do I properly identify each of the files for manipulation?
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Simple Example to Explain Defining all Open Workbooks

    It is easier to open the workbook via a macro. You can assign the Object after each is opened:

    Please Login or Register  to view this content.
    Gary's Student

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Simple Example to Explain Defining all Open Workbooks

    if you know the full paths to each file you can use getobject to return a reference to them

    if not you will require api calls to locate each excel window (XLMAIN) then iterate the workbook window handles within-you can use the accessibility objects to convert a window handle to an automation object
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,466

    Re: Simple Example to Explain Defining all Open Workbooks

    if you know the full paths to each file you can use getobject to return a reference to them

    if not you will require api calls to locate each excel window (XLMAIN) then iterate the workbook window handles within-you can use the accessibility objects to convert a window handle to an automation object

    That's one of those sentences where I understand all the words, or at least the great majority, but, joined together, it's just like a really loud buzzing in my head.

    Care to elucidate?

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Simple Example to Explain Defining all Open Workbooks

    +1 @ TM. I need to see a sample script to understand the API calls.

    Expanding on OP, the scenario is driven off a vbYesNo whereby user is asked if the data they want to retrieve is in an open workbook (which theoretically could be an open workbook in "active" instance of Excel, or another instance, which may include one or many other workbooks); or, if "no", .GetOpenFileName to a closed workbook.

    I have the closed workbook via ADO method working. I am now trying to understand how to ask the user to pick which of all open workbooks, despite instance of excel, contains their data, set a path, and then consume.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Simple Example to Explain Defining all Open Workbooks

    gee I was hoping nobody would ask ;-)

    I don't have complete code to hand for that-but can probably put it together at lunch time if work clears a little

    this is the code to get an Application object from a workbook handle (orig from stackoverflow I think)

    Please Login or Register  to view this content.
    then you just need a loop with findwindowex looking for XLMAIN windows then within each get the XLDESK window and finally one of the child EXCEL7 windows which you can pass to that function

    easy as pie :-)

  7. #7
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Simple Example to Explain Defining all Open Workbooks


  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,466

    Re: Simple Example to Explain Defining all Open Workbooks

    And, when the fairy dust settles ...

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Simple Example to Explain Defining all Open Workbooks

    like so-including proper attrib to stackoverflow. needs a reference to the scripting runtime as written but could easily be late bound
    Please Login or Register  to view this content.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Simple Example to Explain Defining all Open Workbooks

    if you're lazy you could also automate word and use its Tasks collection ;-P

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,466

    Re: Simple Example to Explain Defining all Open Workbooks

    @JP: oh, now you're taking the p.... mickey

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Simple Example to Explain Defining all Open Workbooks

    :-)
    hey it wasn't me that decided only one instance of excel should get registered in the ROT

+ 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