防止在“保存之前”事件期间关闭

时间:2018-09-21 21:51:13

标签: excel vba

问题:

用户尝试关闭对其进行更改的文件。出现提示询问他们是否要保存,然后单击“是”。如果BeforeSave事件中有代码取消保存,是否有办法也取消关闭?

背景:

我们经常使用“空白文件”,带有公式的预格式化文件,以便用户可以打开,输入数据并以新文件名保存。这是导致问题的最后一步。用户会将其数据保存到空白文件,这意味着我们必须将其恢复到原始状态(清除数据,重新输入已删除/覆盖的公式,将更改恢复为格式等)。

为防止这种情况,我将以下代码添加到所有空白文件中:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If instr(UCase$(ThisWorkbook.Name), "BLANK") <> 0 then
        If SaveAsUI = False Then
            MsgBox "This is the blank file. You can't save it. " _
              & "Please choose 'Save As' and save with a new file name.", _
              vbExclamation, "Can't Save Blank"
            Cancel = True
        End If
    End If
End Sub

如果用户尝试将数据保存到空白文件,他们将收到一条消息,告知他们不能,并使用“另存为”对话框。如果需要更改空白文件,可以使用“另存为”对话框使用相同的文件名进行保存。

问题出在我更改空白文件并尝试关闭该文件时。出现提示,询问我是否要保存更改。不用考虑,我将单击“是”。然后,将触发我的代码,阻止保存并通知我。但是,当我在邮件上单击“确定”时,该文件将立即关闭而不保存更改。

我想要的是一种在保存被取消时防止文件关闭的方法。

1 个答案:

答案 0 :(得分:1)

我从那里获取了代码并对其进行了修改。

save as dialog excel code

我希望它可以解决您的问题,或者至少在进行一些小改动后才能解决。

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim varResult As Variant
    Dim ActBook As Workbook

    'displays the save file dialog
    varResult = Application.GetSaveAsFilename(FileFilter:= _
             "Excel Files (*.xlsx), *.xlsx", Title:="Save As", _
            InitialFileName:=Application.ActiveWorkbook.Path)

    'checks to make sure the user hasn't canceled the dialog
    If varResult <> False Then
        ActiveWorkbook.SaveCopyAs Filename:=varResult '_
        'FileFormat:=xlWorkbookNormal
        Exit Sub
    End If

End Sub


' No it is just in case the user doesn't want to save the file and cancels the first dialog

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If InStr(UCase$(ThisWorkbook.Name), "BLANK") <> 0 Then
        If SaveAsUI = False Then
            MsgBox "This is the blank file. You can't save it. " _
              & "Please choose 'Save As' and save with a new file name.", _
              vbExclamation, "Can't Save Blank"
            Cancel = True
        End If
    End If
End Sub

编辑:

按下关闭按钮后:

  • 每次更改窗口时都会弹出一个“另存为”的窗口,保存副本后它会关闭,之后便不再弹出窗口了。
  • 如果未进行任何更改,则会关闭工作簿

代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim varResult As Variant
    Dim ActBook As Workbook

    If InStr(UCase$(ThisWorkbook.Name), "BLANK") <> 0 Then
        If Not ThisWorkbook.Saved Then

            'displays the save file dialog
            varResult = Application.GetSaveAsFilename(FileFilter:= _
                     "Excel Files (*.xlsm), *.xlsm", Title:="Save As", _
                    InitialFileName:=Application.ActiveWorkbook.Path)

            'checks to make sure the user hasn't canceled the dialog
            If varResult <> False Then
                ActiveWorkbook.SaveCopyAs Filename:=varResult '_
                'FileFormat:=xlWorkbookNormal
                ThisWorkbook.Saved = True
                Exit Sub
            Else
                ThisWorkbook.Saved = True
            End If

        End If
    End If

End Sub


' Just in case the user presses save in options

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If InStr(UCase$(ThisWorkbook.Name), "BLANK") <> 0 Then
        If SaveAsUI = False Then
            MsgBox "This is the blank file. You can't save it. " _
              & "Please choose 'Save As' and save with a new file name.", _
              vbExclamation, "Can't Save Blank"
            Cancel = True
        End If
    End If
End Sub

最终版本

按下关闭按钮后:

  • 每次更改都会出现一个窗口,询问您是否要保存更改(可以取消更改,说“是”或“是”)

  • 如果选择了“是”(默认按钮),则会弹出一个“另存为”窗口,保存副本后它会关闭,之后不再弹出窗口

  • 如果未进行任何更改,则会关闭工作簿

代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim varResult As Variant
    Dim ActBook As Workbook
    Dim MsgBoxAnswer As Variant

    If InStr(UCase$(ThisWorkbook.Name), "BLANK") <> 0 Then
        If Not ThisWorkbook.Saved Then

            MsgBoxAnswer = MsgBox("Do you want to save changes?", vbYesNoCancel + vbExclamation + vbDefaultButton1, "Microsoft Office Excel")

            If MsgBoxAnswer = vbYes Then


                MsgBox "This is the blank file. Save it with a new file name.", _
                vbExclamation, "Can't Save Blank"

                'displays the save file dialog
                varResult = Application.GetSaveAsFilename(FileFilter:= _
                         "Excel Files (*.xlsm), *.xlsm", Title:="Save As", _
                        InitialFileName:=Application.ActiveWorkbook.Path)

                'checks to make sure the user hasn't canceled the dialog
                If varResult <> False Then
                    ActiveWorkbook.SaveCopyAs Filename:=varResult '_
                    'FileFormat:=xlWorkbookNormal
                    ThisWorkbook.Saved = True
                    Exit Sub
                Else
                    ThisWorkbook.Saved = True
                End If

            ElseIf MsgBoxAnswer = vbNo Then

                ThisWorkbook.Saved = True

            ElseIf MsgBoxAnswer = vbCancel Then

                Cancel = True

            End If

        End If
    End If

End Sub


' Just in case the user presses save in options

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If InStr(UCase$(ThisWorkbook.Name), "BLANK") <> 0 Then
        If SaveAsUI = False Then
            MsgBox "This is the blank file. You can't save it. " _
              & "Please choose 'Save As' and save with a new file name.", _
              vbExclamation, "Can't Save Blank"
            Cancel = True
        End If
    End If
End Sub