+ Reply to Thread
Results 1 to 31 of 31

Workbooks.open fails when run from macro

  1. #1
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Workbooks.open fails when run from macro

    Excel 2019 Windows 10

    Have a large wb that takes maybe 6 seconds to unpack itself. Been running it from a macro for years under excel 2007 without much issue

    Now, it randomly (but MOST of the time) simply fails to complete loading and hangs Excel when run that way (not responding, has to be force closed)

    When the same file is run from the Excel file manager, no problem. opens with no errors

    called with :
    Workbooks.Open filespec

    simple call is used, no vars, no objects.

    Tried a bunch of things, all seem to fail. Funny thing is, it sometimes works, then the next day will fail again with the same code.

    The workbook.open event is not firing when it fails, as I have a 'speak' line in there that would tell me it had.

  2. #2
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    So pretty sure the workbook itself is failing to complete load when called from a macro in personal.xls

    But no problem loaded normally

  3. #3
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Hard to go through all the ideas posted here, may have to hire help

    No one has a clue ?

  4. #4
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    Hard to go through all the ideas posted here, . . .
    Keep up the sarcasm, and you can hope for double the responses.

    Quote Originally Posted by jdop View Post
    . . .No one has a clue ?
    You haven't provided any details the rest of us can check. If your workbook is too sensitive to share, try putting Application.EnableEvents = False just before the Workbooks.Open filespec. Does that allow the workbook to open without Excel hanging? If so, the problem would seem to be Excel needing to run the newly opened workbook's Workbook_Open while the preexisting workbook's macro hasn't completed. FWIW, I've had problems with macros (not event handlers) making changes to cells which cause event handler to run.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,192

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    No one has a clue ?
    You are not getting responses because:

    1. You have a very unusual problem that people haven't seen before. I did some Google searches to find a similar problem and came up dry. There was no point for me to post to tell you that I couldn't find an answer, although I subscribed to the thread.
    2. The description is generic and does not contain any information that can be used to diagnose your problem. There is nothing to indicate why your situation is different than any other VBA that opens another workbook. You don't say what "large wb" means, you don't say whether the file you are opening is macro-enabled, you don't say whether filespec is a variable or how it is set. You included only a single line of code. The problem may be related to more than just that one line of code.
    3. The problem is intermittent and so there are no leads to follow for diagnosis
    4. We are unable to reproduce the problem as described. We don't have your files.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  6. #6
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    All your points are well taken, but I am an experienced programmer, and the info I gave is actually all there is to it.

    Yes, the target WB is macro enabled.

    I tried the suggestion Application.EnableEvents = False

    Doing this prevents Excel from hanging, but the workbook_open event doesn't fire. Application.EnableEvents = True set after the Open line is too late for that as well.

    So , we know then its the workbook_open even that fails , which kind of gets us back to square one.

    Now, in all this experimenting , I can now open the workbook repeatedly (this after closing Excel completely) without error.

    Don't know why this is, but most likely in a day, it will fail to open again . Same file. Yeah, I know its a head scratcher.

    1) Workbook always opens when done from Excel file picker
    2) Workbook sometimes opens when done from macro .
    3) Workbook hangs Excel (not responding) all other times.

  7. #7
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    . . . I tried the suggestion Application.EnableEvents = False Doing this prevents Excel from hanging, but the workbook_open
    event doesn't fire. . . .
    If this other workbook would commonly be opened by other workbooks, put most of the code from that other workbook's Workbook_Open event handler into
    a macro (X) in a general module with that macro NOT marked Private. Have that other workbook's Workbook_Open event handler call X in that workbook,
    and call X AS THE LAST STATEMENT in the macro opening that other workbook. If that doesn't work, maybe use Application.OnTime to launch to run X after
    the macro which opened the other macro has completed. If that also doesn't work, I'm stumped.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Workbooks.open fails when run from macro

    If you are saying that the same macro, called the same way, fails to open your workbook most, but not all, of the time, then the cause is likely to be environmental rather than a code issue. Is there any chance that you have the Shift key held down while the macro is running (eg you're using a shortcut key combination that uses Shift)?
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Code cant be moved outside of workbook. All the calling macro does is look for the last highest version number to run.

    No, there's no shift key or anything like it causing this .

    I didn't even reboot, I put the computer in sleep mode overnight

    Just tried running the same workbook which worked 3 or 4 times in a row last night (using the macro) and it FAILS AGAIN, as expected.

    Very very odd. Excel has some strange ways, this is yet another one of them

    Fortunately, its easy to work around, but would be up the creek if it wasnt

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,192

    Re: Workbooks.open fails when run from macro

    The Windows Event Viewer might give some clues if you see entries for the application. I don't know if it would log it if it hangs, and I am not an expert at interpreting the logs, but it might give information from the environment angle.

  11. #11
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Not likely to be a help here. Usually nothing VBA related will show in the log. I'll look anyhow

  12. #12
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    Code cant be moved outside of workbook. . . .
    Not what I suggested. Instead of

    ThisWorkbook code module
    Please Login or Register  to view this content.
    use

    ThisWorkbook code module
    Please Login or Register  to view this content.
    General code module IN SAME WORKBOOK
    Please Login or Register  to view this content.
    When opened directly, Workbook_Open calls StartUpMacro. When opened by another macro in another workbook with events disabled, that other macro could call the newly opened workbook's StartUpMacro procedure.

  13. #13
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    I already run the opening code via a timed delay, located in the open event. so an a

    Trying your method, I get a compile error in the calling code, if the target workbook is not already open.

  14. #14
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    In trying the last suggestion, I removed the timed delay, and went with the main init code in the workbook_open event .

    Now the file is loading BUT, its throwing an error 438 at the workbook.open line. "Object doesnt support property or method"

    Never gets to the workbook_open event in the newly opened wb.

    Not sure what to make of this as the only object is the newly opened file, and the file throws no error when opened alone .

    All I am passing to the workbook.open method is a simple filespec, no other parms.

    an ON ERROR RESUME NEXT does not prevent the load from stopping

  15. #15
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    dunno, been doing some intense debugging trying to narrow down the exact cause, but the built in error handler pretty much stinks for this .

    cant even access the vba properties of the workbook until the calling macro completes or is ENDED . no way to have VBA tell me what line is offending. set BREAK ON ALL ERRORS , but still breaks only on the workbook.open line , not in the presumed problem area

  16. #16
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    . . . Now the file is loading BUT, its throwing an error 438 at the workbook.open line. "Object doesnt support property or method" . . .
    Workbook.Open doesn't exist. Do you mean Workbooks.Open?

  17. #17
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by hrlngrv View Post
    Workbook.Open doesn't exist. Do you mean Workbooks.Open?
    of course .............

    and the title of this post is clear about that
    Last edited by jdop; 01-28-2020 at 10:01 PM.

  18. #18
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Had the very clever idea of using the Application.FileDialog(msoFileDialogOpen) method to open the file and see if it behaved any better , since doing that manually seems to work

    Sadly, I must misunderstand the genius behind Excel internals , as this action does absolutely nothing. Zilch. I'd have thought that pressing open on that dialog when it pops up would actually open something (so you dont ask, yes , the filename is populated with the InitialFileName property.)

    Silly me!

    After some googling, I see no reference to actually being able to OPEN a file using this method, even though its implied by the name , it simple returns one or more filenames ....

    I know it might be a lot to ask , but a file open that isn't gelded would be nice!
    Last edited by jdop; 01-28-2020 at 11:23 PM.

  19. #19
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Workbooks.open fails when run from macro

    I presume you tried various sorts of rebuilding of the workbook or reduction in its size, did you discover anything interesting from that regarding when it works reliably and when it doesn't?

    does the file and its vba close properly after you close the file? Ie if you go into the projects explorer after it is closed I presume it isn't there right?
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  20. #20
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Sadly it all works properly except for the 438 error when run from a macro.

    This is a non trivial project, with a lot going on, so I suspect its some object thats not in proper focus when the wb is called this way.

    I tried tracking down the offending line using audio, since VBA is like those puzzle games , where it defies you at ever chance. God forbid it would throw an error at the line it doesn't like

  21. #21
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Anyone know how to simulate the FILE/OPEN that you do with the keyboard, since the Application.FileDialog can't hack it.

    I have a feeling that MIGHT work past this .

  22. #22
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    Anyone know how to simulate the FILE/OPEN that you do with the keyboard, . . .
    See Application.SendKeys.

  23. #23
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Im well aware of sendkeys, and best practice is to avoid that if possible

    must be a windows hook into the file open routine

  24. #24
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    Im well aware of sendkeys, and best practice is to avoid that if possible
    We're well into looking for any & all alternatives at this point, no? In general it may be a good idea to avoid SendKeys, but you may have reached the point at which you have to consider using it, no?

    must be a windows hook into the file open routine
    Assuming you want to open the other workbook in the SAME Excel instance, and you're adamantly opposed to using SendKeys, you may have to consider using a VBScript file to call GetObject to get a reference to an active Excel session, then with xlapp set to the resulting Excel application object,

    xlapp.Workbooks.Open your_pathname_here

    where your_pathname_here would be either hard-coded or read from commandline arguments. Call that VBScript file using VBA's Shell statement, which runs other processes asynchronously.

  25. #25
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by jdop View Post
    After some googling, I see no reference to actually being able to OPEN a file using this method, even though its implied by the name , it simple returns one or more filenames ....
    It has an Execute method to actually open the file.

  26. #26
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,347

    Re: Workbooks.open fails when run from macro

    So sounds like you think it is the code before the workbook open and that code is doing lots of switching focus between workbooks?
    sounds like you might be able to edit that reasonably easily and run some tests not performing certain segments.

    I sense hrlngrv is on the same page in that if we were you we would be making a copy and running some tests with whatever we can just to find where the problem is even if they were not actually going to be the final solutions to that problem or even if they essentially broke the file/vba because once you know the issue back to the original and it should be much easier to code around it.

  27. #27
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by rorya View Post
    It has an Execute method to actually open the file.
    Where do you see an Execute for Application.FileDialog ??

  28. #28
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    No need to use sendkeys, easy enough to open the File dialog , which as expected works without crashing Excel.

    No easy way to insert the filename in the filespec box (sendkeys does not work in this object), so have to click on it manually

    The whole problem caused by Excel weirdness and focus issues. Getting errors where VBA cant find name ranges (when opened via macro) , then magically it can .

  29. #29
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Workbooks.open fails when run from macro

    In the documentation: https://docs.microsoft.com/en-us/off...members-office and Intellisense and the Object Browser.

  30. #30
    Registered User
    Join Date
    05-26-2014
    Posts
    49

    Re: Workbooks.open fails when run from macro

    Quote Originally Posted by rorya View Post
    In the documentation: https://docs.microsoft.com/en-us/off...members-office and Intellisense and the Object Browser.
    Indeed. I looked at the docs, and my eyes went to all the properties listed, missing the Execute ! This is why three heads are better than one. It did seem bizarre that there wouldn't be something like this, but in googling it, every example seems to use Workbooks.Open after getting the filespec ! and not Execute.

    Initial testing seems to indicate this process avoids the focus errors I'm running into ,using the Workbooks.Open method.

    Slightly clumsier, but its just an extra keyclick for me.

    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogOpen)

    fd.InitialFileName = Fspec
    fd.Show
    fd.Execute



    I'd have preferred to track down the exact reason for the fail, but this will suffice until I do.
    Thanks for all the advice.


    PS Pretty interesting that this problem even exists, and seems to be rare. Why there would be a major diff between workbooks.open and using the file dialog, is beyond my pay grade.
    Last edited by jdop; 01-29-2020 at 08:21 PM.

  31. #31
    Registered User
    Join Date
    09-15-2019
    Location
    Gurgaon, India
    MS-Off Ver
    2003 2007 2010
    Posts
    8

    Re: Workbooks.open fails when run from macro

    I got big file sometimes take long time to open . Rarely also crash does on open . Also with Worbooks.Open sometimes crashes

    Funny is, if workbook is closed and I use Appication.Run to start a macro in it,
    it opens quicker then also and so far never crashed..

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Skip if Workbooks.Open fails to open the file
    By mhraja in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2015, 06:50 PM
  2. workbooks.open fails with no error
    By steve99g in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-27-2013, 12:09 PM
  3. Replies: 14
    Last Post: 08-17-2012, 10:54 AM
  4. Workbooks.Open fails
    By Juuljus in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-12-2006, 05:10 AM
  5. Replies: 1
    Last Post: 01-02-2006, 11:30 PM
  6. Replies: 0
    Last Post: 12-30-2005, 04:35 AM
  7. Workbooks.open method fails
    By JAT in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2005, 05:06 PM

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