Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SaveAs in BeforeSave crashing 1

Status
Not open for further replies.

DrSimon

IS-IT--Management
Dec 14, 2001
674
GB
The following code which asks the user if he wants the version changed before saving, works perfectly well, apart from the fact that it crashes with a 'Microsoft Excel has stopped working' error message when quitting the subroutine. I've trapped it and it crashes after executing End Sub.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)]
    Dim Ver As String
    Dim Vern As Single
    Dim Ans As Variant
    Dim FName As String
    Dim FPath As String
    Ans = MsgBox("Do you want to up-version before saving?" & vbCrLf & "Alternatively Cancel to not save at all.", vbQuestion + vbYesNoCancel)
    Select Case Ans
    Case vbCancel
        Cancel = True
        Exit Sub
    Case vbNo
        Exit Sub
    Case vbYes
        ' Want to keep decimal places in version
        Ver = ActiveWorkbook.CustomDocumentProperties("Version")
        Vern = Val(Ver) + 0.01
        Ver = Str(Vern)
        ActiveWorkbook.CustomDocumentProperties("Version") = Ver
        FPath = ActiveWorkbook.Path
        FName = FPath & "\Audit_v" & Ver & ".xlsm"
        Application.EnableEvents = False
        ActiveWorkbook.SaveAs Filename:=FName
        Application.EnableEvents = True
End Sub

Has anyone any idea what the problem is or failing that, an alternative solution?

Thanks
Simon
 
Cancel the event for vbYes too:
Code:
...
Case vbYes
    ' Want to keep decimal places in version
    Cancel = True
    Ver = ActiveWorkbook.CustomDocumentProperties("Version")
    Vern = Val(Ver) + 0.01
    Ver = Str(Vern)
    ... etc.

combo
 
Thanks very much - simple solution
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top