+ Reply to Thread
Results 1 to 4 of 4

Weird behaviour of the BeforeSave procedure when fired by Me.Save

  1. #1
    Registered User
    Join Date
    04-28-2011
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    3

    Weird behaviour of the BeforeSave procedure when fired by Me.Save

    Hi there fellow Excel users,

    I'm pretty new to VBA (but studied C++ and other languages).

    I've come accross a problem with a BeforeSave procedure not executing properly when fired by Me.Save as part of a BeforeClose procedure.

    Consider the piece of code below.

    When manually saving (through Ctrl+S), the Workbook_BeforeSave event is fired and everything works as expected.

    However, firing the BeforeClose event does not bring the expected result. The "Me.Save" line in the BeforeClose effectively fires the Workbook_BeforeSave event, as confirmed by the MsgBox that pop ups, but the two ThisWorkbook.Worksheets().Visible lines are not getting executed.

    I don't quite understand why it's acting like this, as the code seem pretty simple and logical to me. Is there some special restriction that I wouldn't know about event procedures that would prevent this piece of code from working properly?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,255

    Re: Weird behaviour of the BeforeSave procedure when fired by Me.Save

    Hi and welcome to the forum.

    Yes, this is a restriction - basically the Workbook class's methods are blocked once the Workbook.Save call from the Workbook_BeforeClose event handler is made. We had a good chat about what might cause this behaviour here and the best idea we had was that it is designed to prevent infinite recursion. As I mentioned on that thread, I was never 100% comfortable with that theory because the same restriction isn't applied in, for example, MS Word.

    To workaround it you can temporarily disable events in the Workbook_BeforeClose event handler (to prevent Workbook_BeforeSave being called at al) and hide the sheets within the Workbook_BeforeClose event handler.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Weird behaviour of the BeforeSave procedure when fired by Me.Save

    Welcome to the forum.

    What happens when you step throgh the code?

    Where is ClosingWorkbookFlag (which shood be a Boolean) declared?
    Entia non sunt multiplicanda sine necessitate

  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,258

    Re: Weird behaviour of the BeforeSave procedure when fired by Me.Save

    Here is another link that provides some more insight in to this problem
    http://www.dailydoseofexcel.com/arch...rereallyclose/
    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!)

+ 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