MsOf07 [RESOLVED] Strange behavior - background Excel-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] Strange behavior - background Excel

  1. #1

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    590

    Resolved [RESOLVED] Strange behavior - background Excel

    I have a vbscript file that launches Excel in the background, opens a workbook and executes a macro (takes 1hr to execute the macro).

    While the background instance of Excel is running, if I open a different workbook in the foreground by double clicking the file name, for some reason the workbook in the background instance of Excel becomes visible and the vbscript aborts.

    I can get around this with a two-step process by launching Excel (via shortcut) then opening the desired workbook, but why would this be needed?

    I don't understand why opening a (different) workbook in the foreground would mess up a background instance of Excel.

    Comments?
    Last edited by VBAhack; Feb 21st, 2012 at 03:03 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    19,997

    Re: Strange behavior - background Excel

    opening another workbook, when one is already running could open in the same or a different instance of excel
    it would appear opening excel by shortcut will open in a different instance of excel, whereas double clicking a file will try to open in an existing instance
    you can keep your hidden workbook hidden by setting its primary window to visible = false when opening
    vb Code:
    1. xlapp.windows(1).visible = false
    change app object to suit

    whether keeping it hidden will allow the script to keep running or still fail i would be doubtful about, but you should be able to test what stops the script from running when the new workbook opens, are you using any excel events in your script?


    there would be some other things you could try, but you will have to test to see if it can work
    your script could create 2 instances of excel, in one open your workbook, lave the other for any additional workbooks to open in, you would have to test, to find if other workbooks open in the first created instance or the last, then open your book in the other, of course any other book opened may open in the last used
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    590

    Re: Strange behavior - background Excel

    Thanks for the suggestions. Launching a 2nd instance of Excel seems to work. Opening subsequent files appears to use the latter instance. I just have to be careful to not kill the 2nd instance in the vbscript file like I do the first one!

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    19,997

    Re: [RESOLVED] Strange behavior - background Excel

    you can count the open workbooks in 2nd instance, if none then quit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.