+ Reply to Thread
Results 1 to 7 of 7

How to open additional workbooks WITHOUT taking focus?

  1. #1
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Question How to open additional workbooks WITHOUT taking focus?

    Ok, the problem I have is this.
    I have a Workbook that is a 'summary' workbook.
    This workbook opens several other workbooks, one at a time, writes values to them, reads values back from them, enters those values into the 'summary' workbook, and then saves & closes the individual workbook.
    This is repeated perhaps 50 times or so, ideally I want to be able to start the macro and then do something else whilst Excel sits peacefully in the background doing it's thing.

    However, it doesn't seem to want to! As soon as I open a new workbook the new workbook seems to get the focus, then instantly pass it back to the summary workbook, (which you can see by watching the buttons on the start bar if you step through the code).

    The problem I am getting is that if I start the macro going, then go to another application and do something that involves me holding down the shift key for a period of time, then the Excel macro just stops. It doesn't stop on a particular line according to the VBA window, it just finishes, but it is only halfway through.

    As an example to what happens I have the following code.
    First create 2 blank excel workbooks in a folder, one called 'Main.xls' and one called '0 - Main.xls'
    In 'Main.xls' insert a code module with the following:

    Please Login or Register  to view this content.
    The 'setup_init' macro will setup 50 workbooks to use as samples.

    Then run the 'test' macro.
    This will obviously just open each workbook, enter a value, and close it again (saving the change).

    If you just let the macro run then all is fine, but if once you have started it you switch to another application, (I used Outlook), and then hold the left shift to select multiple messages MOST the time the Excel macro stops.

    I can only think this is due to it somehow catching the 'shift' and that interrupting the macro somehow.

    I know there is a VBA command that you can use to stop Excel accepting ANY keyboard or mouse input, but I don't like to use that because if something unexpected happens you have no way (that I know of) of getting back into Excel.

    Any ideas?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    I have not tried this, but perhaps
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    07-01-2008
    Location
    Cincinnati, OH
    Posts
    150
    Along the same lines as SHG. The code you listed should be called from another workbook. We'll just call it Controller.xls. Controller.xls creates a separate instance of Excel, opens the workbook that does the processing in this instance, which in turn processes your many workbooks. The instance should have its Application.IgnoreRemoteRequests set to True and its Application.UserControl property set to False. This instance also runs invisibly in the background. As for losing an invisible session of Excel if something goes wrong, you provide callbacks to Controller.xls. Controller.xls can respond to any problems. This may sound overly complicated, but it is not. You are basically using Excel as an ActiveX.exe server which is similiar to a DLL except it runs in another process leaving you alone in your visible session unless Controller.xls is notified of problems and eventual successful completion of the task. If you are interested and will stick with me through several more posts, I'll walk you through it...

  4. #4
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Smile

    Sounds interesting, in particular the callbacks & notifications.

    If you are willing to walk through it I'm more than happy to sit and take notes

    Thankyou!

  5. #5
    Forum Contributor
    Join Date
    07-01-2008
    Location
    Cincinnati, OH
    Posts
    150
    Hi Phil. I used your example to create an example.

    If you need more clarification, please reply with any questions.

    In summary, Controller.xls creates a hidden Excel instance, opens FileProcessor.xls within, and initiates several routines.

    Controller.xls runs a procedure named "Setup_Init" within FileProcessor.xls. This procedure creates 50 new workbooks and calls back Public Sub ProgressUpdate in Controller.xls after each workbook is created. The progress is reported in the statusbar of Controller.xls parent application.

    When Setup_Init has completed, FileProcessor.xls notifies Controller.xls by calling back Public Sub InitComplete() within Controller.xls.

    Controller.xls, from within this callback, runs another procedure in FileProcessor.xls named "Test".

    Just a note to add. The use of the statusbar will cause you problems. If you need a visual progress cue, use a modeless userform.

    The "Test" procedure open, writes to, saves, and then deletes each of the 50 new workbooks and calls back Public Sub ProgressUpdate in Controller.xls after each workbook is processed. The progress is reported in the statusbar of Controller.xls' parent application.

    I created an error condition by deleting a file in the loop. The error handling logic is there for you to build upon. It can get quite complex. Basically, you pass back a return value to instruct FileProcessor.xls from the callback, "Public Sub OOPSError", as to the steps it should take for any given error. In the example, we just tell FileProcessor.xls to go on to the next workbook.

    After test has completed, Public Sub Complete is called back in Controller.xls. We clean up in this procedure by closing FileProcessor.xls and the hidden instance of Excel.

    Download the example and open Controller.xls. Click on the run button and watch the statusbar to see the flow logic. You will recieve several msgboxes along the way. Just click 'Ok' to continue. You will need to edit the path to FileProcessor.xls before running.

    Example Download


    To create this project...

    Create a folder and and a new workbook within named, for the sake of clarity in this example, FileProcessor.xls. This is the workbook that will do the processing of your many files. Place this code in a standard module. Add a reference to MS Scripting.

    Please Login or Register  to view this content.

    Outside of this folder that contains FileProcessor.xls, create the workbook that will be visible and control, so to speak, FileProcessor.xls. Name it Controller.xls. Place this code into the workbook class.

    Please Login or Register  to view this content.
    Edit the path to FileProcessor.xls in this line:
    Set OOPSWb = OOPSApp.Workbooks.Open("path of OOPWB.xls")

    BTW. OOPS is there for Out Of Process Server. I do expect this to work as I have used this method for years to asynchronously update various workbooks or to just run lengthy procedures in another process. If you have access to a development environment such as VB, you can create an ActiveX.exe for better stability though I have not had any problems thus far using the method prescribed here.

    The ontime hack simply releases the client. In this example, Controller.xls. Without the several ontime calls, Controller.xls would have to wait upon FileProcessor.xls for each procedure called. Using this method allows you to go about your business while FileProcessor.xls does the work in the background and notifies Controller.xls at various intervals along the way. It is not neccesary to callback to the extent that I did in this example. Normally, the client is only called back once. At the completion of some lenghty task. Such as, DataAvailable or QueryComplete.

    Anyway, I hope that I was able to spell this out clearly without too much confusion...

    Just a note to add. The use of the statusbar can cause you problems. If you require a visual cue of progress for some lengthy task, use a modeless userform...
    Last edited by Tom Schreiner; 07-29-2008 at 10:35 AM.

  6. #6
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Thumbs up

    At my first read through that seems very informative and well written, thank you!
    I shall try it out hopefully this evening and let you know how I get on, (once I have adjusted my VBA to run in this 'staggered' formation, (ie. using the 'controller' level).

  7. #7
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Lightbulb

    I must admit I have not yet had a chance to implement this 'controller' scheme as I have been tied up on the main project.

    However as an interesting note I stumbled across this today which seems to exactly describe the problem (and workaround) that I was having, (albeit that I didn't know thats where it was stopping!):

    Macro in Excel Stops After A Workbook.Open Command

    Just thought I'd link it in here as it is relevant

+ 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