+ Reply to Thread
Results 1 to 22 of 22

Workbook_BeforeSave problems with saving

Hybrid View

Steve@Rugby Workbook_BeforeSave problems... 03-06-2014, 06:27 AM
TMS Re: Workbook_BeforeSave... 03-06-2014, 06:58 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 07:21 AM
Leon V (AW) Re: Workbook_BeforeSave... 03-06-2014, 07:36 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 07:51 AM
Leon V (AW) Re: Workbook_BeforeSave... 03-06-2014, 08:06 AM
TMS Re: Workbook_BeforeSave... 03-06-2014, 08:41 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 09:01 AM
Leon V (AW) Re: Workbook_BeforeSave... 03-06-2014, 09:10 AM
Izandol Re: Workbook_BeforeSave... 03-06-2014, 09:11 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 09:18 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 09:31 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 09:25 AM
Leon V (AW) Re: Workbook_BeforeSave... 03-06-2014, 09:38 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 10:21 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 09:46 AM
Leon V (AW) Re: Workbook_BeforeSave... 03-06-2014, 10:24 AM
Izandol Re: Workbook_BeforeSave... 03-06-2014, 10:38 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 11:26 AM
Izandol Re: Workbook_BeforeSave... 03-06-2014, 11:41 AM
Steve@Rugby Re: Workbook_BeforeSave... 03-06-2014, 12:04 PM
Steve@Rugby Re: Workbook_BeforeSave... 03-11-2014, 04:12 AM
  1. #1
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Workbook_BeforeSave problems with saving

    Hi,

    I have a problem with some of my code which has baffled me for ages & was hoping someone can help me resolve the issue.

    I have a BeforeSave macro that works ok when you save manually, however there are 2 other instances that do not work:

    1) Using "ThisWorkbook.Save" in another macro does not save the changes. (when you close the workbook and answer no to saving changes you lose changes that were made before running the "ThisWorkbook.Save")

    2) If i have a 2nd workbook open at the same time I get stuck in an eternal loop when asked if i want to save changes when closing the workbook. (just keep getting the dialog box asking if i want to save changes, click yes and it comes back)

    I hope I have explained this clearly and really do appreciate any advice that can be given

    BeforeSave Code:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
       
    Application.EnableEvents = False
    Application.ScreenUpdating = False
        
    '************************************************************************************************
    If SaveAsUI = True Then ' If user tries to SaveAs then cancel
        Cancel = True
        MsgBox "You cannot use 'Save As' with this workbook.  Use 'Save'", vbCritical, "Cannot Save As"
    '************************************************************************************************
    Else ' Save workbook in opening workbook state
    ' Cancel original save request
        Cancel = True
    ' Unprotect worksheets
        Call UnProtect_Worksheets
    ' Unprotect workbook
        Call UnProtect_Workbook
    ' Get current worksheet name
        Dim CurrentSheet As String
        CurrentSheet = ActiveSheet.Name
    ' Select Open Message worksheet & hide all other worksheets
        Dim s As Worksheet
        Const wsKeep As String = "Open Message"
        Worksheets(wsKeep).Visible = True
        Worksheets(wsKeep).Select
        For Each s In Worksheets
            If s.Name <> wsKeep Then s.Visible = xlSheetHidden
        Next s
    ' Make Enable Macro Instructions worksheet visible
        Sheets("Enable Macro Instructions").Visible = True
    ' Protect worksheets
        Call Protect_All_Worksheets
    ' Protect workbook
        Call Protect_Workbook
    ' Save this workbook
        ThisWorkbook.Save
    '************************************************************************************************
    ' Return to worksheet that was open before save
    ' Unprotect worksheets
        Call UnProtect_Worksheets
    ' Unprotect workbook
        Call UnProtect_Workbook
    ' Select worksheet that was open when save was selected by user
        Sheets("" & CurrentSheet & "").Visible = True
        Worksheets("" & CurrentSheet & "").Select
    ' Hide Enable Macro Instructions & Open Message worksheets
        Sheets("Enable Macro Instructions").Visible = False
        Sheets("Open Message").Visible = False
    ' Protect worksheets
        Call Protect_All_Worksheets
    ' Protect workbook
        Call Protect_Workbook
    '************************************************************************************************
    End If
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    
    End Sub
    Regards
    Steve
    Last edited by Steve@Rugby; 03-06-2014 at 06:46 AM.

  2. #2
    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,549

    Re: Workbook_BeforeSave problems with saving

    I suspect that this may be a problem:

    ' Save this workbook
        ThisWorkbook.Save
    '************************************************************************************************
    You are in an event handler, the BeforeSave event, in fact, and are trying to save the workbook. I would expect that to fire the BeforeSave event again.

    I would suggest that you try:

    ' Save this workbook
        Application.EnableEvents = False
        ThisWorkbook.Save
        Application.EnableEvents = True
    '************************************************************************************************

    No guarantees, but it should help. Try putting some Stops in the events and stepping through to see what happens.


    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


  3. #3
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi TMS,

    Thanks for the advice I have tried your suggestion but I still have the same problem

    Not sure if you could confirm for me but I think I have already dealt with stopping the event handler in the 1st line & last line of the code i attached?

    Sorry I dont think my explanation was very clear, when i mentioned "ThisWorkbook.Save" does not save, I meant when it is used in another module. I have stepped through when using this ThisWorkbook.Save from another module & it fires the beforesave event which I see the code running with no errors and in exactly the same way as if I pressed the save button in the quick access menu (which does work & is what i meant by manual save).

    Thanks again for you help

    Regards
    Steve

  4. #4
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Workbook_BeforeSave problems with saving

    Is the other module in PERSONAL.XLSB or another sheet? ThisWorkbook refers to the workbook the macro is writen in.

    also get rid of
    ' Cancel original save request
        Cancel = True
    This Is the original save request you are just executing some code before it saves.
    Last edited by Leon V (AW); 03-06-2014 at 07:41 AM.
    Sub Reputation()
    Dim Problem as Variant
    Dim Reputation as Integer
    For Each Problem in Forum.Threads
        If Problem.Title = "*[Solved]*" and Solver.Name = "Leon V (AW)" Then Reputation = Reputation + 1
    Next Problem
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi Leon,

    Thanks for yor advice.

    All the code is in the same workbook none are in personal.xlsb.

    I had canceled the save at the begining as i wanted to set the workbook back to a certain 'open workbook state' before the save happens, then save after open state is set. The reason I have done this is so if the workbook is opened by a user with there excel set to disable macros they see the page that the before save event has set (It just tells them to turn on macros & gives instructions on how to do that).

    Thanks you & Kind regards
    Steve

  6. #6
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Workbook_BeforeSave problems with saving

    Quote Originally Posted by Steve@TRW View Post
    Hi Leon,
    I had canceled the save at the begining as i wanted to set the workbook back to a certain 'open workbook state' before the save happens, then save after open state is set. The reason I have done this is so if the workbook is opened by a user with there excel set to disable macros they see the page that the before save event has set (It just tells them to turn on macros & gives instructions on how to do that).
    But this is a BEFORE SAVE event so all the code in that macro will run before excel saves the cancel = true doesn't stop the save there and then.
    when a user clicks save:
    Excel "I would like to save",
    VBA "ok but first do this" (your Workbook_BeforeSave runs)
    Excel "now can I save?",
    VBA "cancel = true, so no"
    Last edited by Leon V (AW); 03-06-2014 at 08:09 AM.

  7. #7
    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,549

    Re: Workbook_BeforeSave problems with saving

    My apologies. I completely glossed over that! I would, however, be inclined to limit switching the event handling off and on to the immediate area that would trigger the event.

    The other thing that can happen is that you can switch event handling off and on in an "outer" module and do the same in an "inner" module. Depending what the subsequent code in the outer module does, event handling may have been switched on earlier than you had intended.

    I think that, because you set Cancel=True, you are effectively cancelling the save ... hence, when the user says, "no, don't save", that's exactly what happens.

    Probably more random ideas than a concrete suggestion ...

    Regards, TMS

  8. #8
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi Leon,

    I understand what you are saying however if i remove the cancel command from the before save event, i do not get the required result.

    If i leave it in, it works as required when i manually save, just not when I saving calling the ThisWorkBook.Save in a seperate module.


    The BeforeSave event is doing the following:

    1) Cancel the original save (I do this as I do not want to save all of the BeforeSave event)

    2) Take note of current active worksheet

    3) Show/select "open worksheet" & hide all others

    4) Now save with ThisWorkBook.Save (enable events is set to false so wont recall BeforeSave event again)

    5) After saving, return to worksheet that was open before save and hide "open worksheet".

    Result is that user has saved workbook in the required open workbook state then after saving is returned to worksheet they were on when they saved so they could if required continue editing workbook. If i did not cancel original save, each time a save was pressed a user would be sent back to the "open worksheet".

    Regards
    Steve

    PS please try not to get annoyed if I am still not understanding what you are saying. I do appretiate your help and do not want to offend/irratate you or anyone else thet is good enough to give up their time to help me.
    Last edited by Steve@Rugby; 03-06-2014 at 09:09 AM. Reason: Peace keeping comment added

  9. #9
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Workbook_BeforeSave problems with saving

    do any of your Call macros end with enable events = true?

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Workbook_BeforeSave problems with saving

    Did you test if BeforeSave event is being raised after ThisWorkbook.Save call?
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  11. #11
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi Leon,

    No none of the call events end with enable events = true.

    Hi Izandol,

    Yes the BeforeSave event is called after ThisWorkbook.Save call.

    I think it would be helpful if I will spent a bit of time removing the sensitive work related stuff from a copy of my workbook and post it on this thread for you to see?

    Regards
    Steve

  12. #12
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi TMS,

    I would, however, be inclined to limit switching the event handling off and on to the immediate area that would trigger the event.
    I will take your advice, I will change my code as you suggest.

    There is only one other macro that turns event handling off/on, it is not the calling macro however as a trouble shooting task I have removed it from the equasion and still have same problems.

    Regards
    Steve

    As mentioned above I will attach the workbook to a post soon as this may be the best way for someone to help.

  13. #13
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Just to clarify as mentioned above, save when closing workbook does work with this BeforeSave event its just when another workbook is open it gets stuck in an eternal loop.

  14. #14
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Workbook_BeforeSave problems with saving

    Quote Originally Posted by Steve@TRW View Post
    Just to clarify as mentioned above, save when closing workbook does work with this BeforeSave event its just when another workbook is open it gets stuck in an eternal loop.
    Excel like most programs has ways of recognising infinite loops and getting out of them it is possible that it just can't recognise the loop with two books open rather than it only existing with two open.

  15. #15
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi,

    Offending workbook attached.

    The ThisWorkBook.Save is called from ''Private Sub Submit_CommandButton_Click'' in the ''Log_Sample_UserForm''. I have added a msgbox to appear onscreen at the point the save is about to happen.

    If you run the "Log a Sample for Archive" userform from the home page you can see the entire process we are taking. (the userform populates worksheet "Sample Log" at next available empty row) then after closing userform the save is attempted using "ThisWorkBook.Save".

    Regards
    Steve
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi

    Just had a thought that may resolve one of my issues.

    Is there a code that i could put in an Open event in this workbook that makes it open in a new instance of excel (rather than on top of any existing open workbooks)?

    Regards
    Steve

  17. #17
    Forum Contributor
    Join Date
    06-22-2011
    Location
    somerset
    MS-Off Ver
    365
    Posts
    330

    Re: Workbook_BeforeSave problems with saving

    Quote Originally Posted by Steve@TRW View Post
    Hi

    Just had a thought that may resolve one of my issues.

    Is there a code that i could put in an Open event in this workbook that makes it open in a new instance of excel (rather than on top of any existing open workbooks)?

    Regards
    Steve
    No unfortunately Application has to be open first as the platform for any macro execution and alternate instances cannot interact. You could write code to open another instance of excel but it would not be able to open a workbook in that instance.

    however having said that there may be some shell comand you could write that has a shot at this.
    Last edited by Leon V (AW); 03-06-2014 at 11:13 AM.

  18. #18
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Workbook_BeforeSave problems with saving

    It is possible to reopen file in separate instance and close current version. You may try:
    Module:
    Sub Highlander()
       Dim wb                          As Workbook
       Dim lCounter                    As Long
       If Application.Workbooks.Count > 1 Then
          For Each wb In Application.Workbooks
             If wb.Windows(1).Visible And wb.Path <> "" Then lCounter = lCounter + 1
          Next wb
          If lCounter > 1 Then
             ThisWorkbook.ChangeFileAccess xlReadOnly
             With CreateObject("Excel.application")
                .EnableEvents = False
                .Workbooks.Open ThisWorkbook.FullName
                .Visible = True
                .EnableEvents = True
             End With
             ThisWorkbook.Close False
          End If
       End If
    End Sub
    ThisWorkbook:
    Private Sub Workbook_Open()
       Application.OnTime Now(), "Highlander"
    End Sub

  19. #19
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi Izandol,

    Just tried it & it works great thank you.

    If I may ask 2 questions please...

    1) I get a message pop up asking "Do you want to save changes before switching file status?" is it possible to supress this message as there would be no changes that i need to save when opening?

    2) Can you see a way in which I can intergrate your code with my existing open workbook code? I have tried it at beginning and end but both seem to over write any other open event coding (I end up in last saved state rather tahn required open state)

    Private Sub Workbook_Open()
    
    ' On opening workbook do...
        Application.ScreenUpdating = False
    ' Select Home Page worksheet & hide all other worksheets
        Dim s As Worksheet
        Const wsKeep As String = "Home Page"
        Worksheets(wsKeep).Visible = True
        Worksheets(wsKeep).Select
        For Each s In Worksheets
            If s.Name <> wsKeep Then s.Visible = xlSheetHidden
        Next s
        Application.ScreenUpdating = True
        
     Application.OnTime Now(), "Highlander"
     
    End Sub
    Regards
    Steve

  20. #20
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Workbook_BeforeSave problems with saving

    You may try:
    Sub Highlander()
       Dim wb                          As Workbook
       Dim lCounter                    As Long
       If Application.Workbooks.Count > 1 Then
          For Each wb In Application.Workbooks
             If wb.Windows(1).Visible And wb.Path <> "" Then lCounter = lCounter + 1
          Next wb
          If lCounter > 1 Then
              ThisWorkbook.Saved = True
             ThisWorkbook.ChangeFileAccess xlReadOnly
             With CreateObject("Excel.application")
                .Workbooks.Open ThisWorkbook.FullName
                .Visible = True
             End With
             ThisWorkbook.Close False
          End If
       End If
       ' On opening workbook do...
        Application.ScreenUpdating = False
    ' Select Home Page worksheet & hide all other worksheets
        Dim s As Worksheet
        Const wsKeep As String = "Home Page"
        Worksheets(wsKeep).Visible = True
        Worksheets(wsKeep).Select
        For Each s In Worksheets
            If s.Name <> wsKeep Then s.Visible = xlSheetHidden
        Next s
        Application.ScreenUpdating = True
    End Sub
    with simply:
    Private Sub Workbook_Open()
    
     Application.OnTime Now(), "Highlander"
     
    End Sub

  21. #21
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Izandol,

    Thats brilliant thanks. Works a like a charm...

    I will start another thread with regards to the 2nd issue I have so that I can mark this as solved and give you the reputation.

    Regards
    Steve

  22. #22
    Forum Contributor
    Join Date
    12-19-2012
    Location
    England
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Workbook_BeforeSave problems with saving

    Hi,

    Solved the other issue myself, changed code as below to add:

    Cancel = False
    Now full code works as:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
       
    Application.ScreenUpdating = False
        
    '************************************************************************************************
    If SaveAsUI = True Then ' If user tries to SaveAs then cancel
        Cancel = True
        MsgBox "You cannot use 'Save As' with this workbook.  Use 'Save'", vbCritical, "Cannot Save As"
    '************************************************************************************************
    Else ' Save workbook in opening workbook state
    ' Cancel original save request
        Cancel = True
    ' Unprotect worksheets
        Call UnProtect_Worksheets
    ' Unprotect workbook
        Call UnProtect_Workbook
    ' Get current worksheet name
        Dim CurrentSheet As String
        CurrentSheet = ActiveSheet.Name
    ' Select Open Message worksheet & hide all other worksheets
        Dim s As Worksheet
        Const wsKeep As String = "Open Message"
        Worksheets(wsKeep).Visible = True
        Worksheets(wsKeep).Select
        For Each s In Worksheets
            If s.Name <> wsKeep Then s.Visible = xlSheetHidden
        Next s
    ' Make Enable Macro Instructions worksheet visible
        Sheets("Enable Macro Instructions").Visible = True
    ' Protect worksheets
        Call Protect_All_Worksheets
    ' Protect workbook
        Call Protect_Workbook
    ' Save this workbook
    
    
        Cancel = False <-------------------------------ADDED THIS LINE
    
    
    
        Application.EnableEvents = False
        ThisWorkbook.Save
        Application.EnableEvents = True
    
    '************************************************************************************************
    ' Return to worksheet that was open before save
    ' Unprotect worksheets
        Call UnProtect_Worksheets
    ' Unprotect workbook
        Call UnProtect_Workbook
    ' Select worksheet that was open when save was selected by user
        Sheets("" & CurrentSheet & "").Visible = True
        Worksheets("" & CurrentSheet & "").Select
    ' Hide Enable Macro Instructions & Open Message worksheets
        Sheets("Enable Macro Instructions").Visible = False
        Sheets("Open Message").Visible = False
    ' Protect worksheets
        Call Protect_All_Worksheets
    ' Protect workbook
        Call Protect_Workbook
    '************************************************************************************************
    End If
    Application.ScreenUpdating = True
    
    End Sub
    Regards
    Steve

+ 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. Replies: 1
    Last Post: 10-25-2013, 06:10 PM
  2. [SOLVED] What is wrong with this code? (Loop through WBs in folder - open, edit, save & close)
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-04-2013, 07:49 PM
  3. [SOLVED] Do - Loop Until loop with randbetween gets stuck
    By supern0va in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2012, 06:06 AM
  4. Replies: 2
    Last Post: 05-04-2012, 04:57 PM
  5. HELP - Stuck in loop
    By gti_jobert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-27-2006, 08:41 AM

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