Hi guys,
I have a mystery which I'm hoping someone will help me solve. I have a workbook which needs to be shared. It relies heavily on VBA functionality.
In order to avoid save conflicts in certain specific cells which don't need to be saved (custom search boxes, stats dropdowns etc) I built a procedure whereby if a user clicks save, a BeforeSave event stores the user's values in variables and clears the contents of those cells; then AfterSave which restores the user's cell values. Effectively, the contents of those specific cells are not saved with the file (therefore no save conflicts), while the user gets to keep what's on their screen after they click save.
While the AfterSave works in Excel 2010, unfortunately my company's network uses Excel 2007, which as I later discovered does not recognise an AfterSave call. The solution, as found elsewhere, is to include everything in the BeforeSave call as follows:
Now here's the problem: while this works perfectly well with a workbook in single-user mode - hijacking the native save function - it fails when the workbook is shared. The line "ThisWorkbook.Save" causes an error stating "The file is locked. Please try again later".
I can see no reason why this should be the case. The weird thing is that the error appears specific to the BeforeSave event. I have also tested it outside the BeforeSave event handler with a simple sub as follows:
... and the above works without an error. So it's not that the Workbook.Save method fails on a shared workbook per se. Just when it's being called BeforeSave.
There are also more complex reasons why I need to use the BeforeSave event, aside from just clearing non-vital cells - including performing custom backups: so it's really not an option to stop using it. I have just distilled it down as above to focus on the problem.
Can anyone advise me? Thanks.
I have a mystery which I'm hoping someone will help me solve. I have a workbook which needs to be shared. It relies heavily on VBA functionality.
In order to avoid save conflicts in certain specific cells which don't need to be saved (custom search boxes, stats dropdowns etc) I built a procedure whereby if a user clicks save, a BeforeSave event stores the user's values in variables and clears the contents of those cells; then AfterSave which restores the user's cell values. Effectively, the contents of those specific cells are not saved with the file (therefore no save conflicts), while the user gets to keep what's on their screen after they click save.
While the AfterSave works in Excel 2010, unfortunately my company's network uses Excel 2007, which as I later discovered does not recognise an AfterSave call. The solution, as found elsewhere, is to include everything in the BeforeSave call as follows:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not ThisWorkbook.MultiUserEditing Then Exit Sub ' Do not do anything special if the "save" is only happening because the WB is being shared.
' DO BEFORE SAVE SUFF HERE
Application.EnableEvents = False ' So the following line will not trigger this same procedure
ThisWorkbook.Save ' Perform the save manually
Application.EnableEvents = True
' DO AFTER SAVE STUFF HERE
Cancel = True ' Cancel the native save function so the workbook isn't saved twice
End Sub
Now here's the problem: while this works perfectly well with a workbook in single-user mode - hijacking the native save function - it fails when the workbook is shared. The line "ThisWorkbook.Save" causes an error stating "The file is locked. Please try again later".
I can see no reason why this should be the case. The weird thing is that the error appears specific to the BeforeSave event. I have also tested it outside the BeforeSave event handler with a simple sub as follows:
Code:
Sub ManualSave()
' To test calling this when the workbook is already shared
ThisWorkbook.Save ' Perform the save manually
End Sub
... and the above works without an error. So it's not that the Workbook.Save method fails on a shared workbook per se. Just when it's being called BeforeSave.
There are also more complex reasons why I need to use the BeforeSave event, aside from just clearing non-vital cells - including performing custom backups: so it's really not an option to stop using it. I have just distilled it down as above to focus on the problem.
Can anyone advise me? Thanks.