更新Workbook_BeforeSave

时间:2016-03-23 12:29:40

标签: excel vba excel-vba

我正在尝试让Excel保存一个具有唯一名称的文件   这将主要在Excel 2003中使用,但也必须在2010年使用。

想法是用户打开模板文件,如果他们点击“保存”或只是关闭工作簿,它将保存为template_1,template_2等。

如果单击“保存”,这样可以正常工作,但如果他们关闭文件,它会询问您是否要保存对原始文件的更改,将其保存在新名称下,然后询问用户是否要保存更改...然后保存并询问用户是否要保存更改等等。显然,我只希望它保存一次然后关闭 - 但事实并非如此。

我尝试将Saved属性设置为TRUE。我在保存后尝试了Cancel = True,但这导致Excel崩溃, Excel遇到了问题,而且确实需要搞定一天类型的消息。

在下面的代码中,我尝试删除Saved=TRUECancel=TRUE,我尝试移动它们 - 在保存之前取消,在保存之后取消但在{{1}之内取消在If...End If代码之前和之后阻止:

EnableEvents

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim NewFileName As String On Error GoTo ERROR_HANDLER NewFileName = GenerateUniqueName(ThisWorkbook.FullName) If NewFileName <> "" Then Application.EnableEvents = False ThisWorkbook.SaveAs NewFileName, ThisWorkbook.FileFormat ThisWorkbook.Saved = True Application.EnableEvents = True End If FastExit: Cancel = True On Error GoTo 0 Exit Sub ERROR_HANDLER: MsgBox "Error " & Err.Number & vbCr & _ " (" & Err.Description & ") in procedure ThisWorkbook.Workbook_BeforeSave." & vbCr & vbCr & _ "DOCUMENT NOT SAVED.", vbCritical + vbOKOnly Application.EnableEvents = True Resume FastExit End Sub 代码如下 - 这假定文件名不包含下划线字符,并将数字附加到文件名为_1,_2等:

GenerateUniqueName

2 个答案:

答案 0 :(得分:2)

请尝试一下,看看你的问题是否解决了?我没有在下面提供你的功能,因为它保持不变。

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Ret As Variant

    If ThisWorkbook.Saved = False Then
        ThisWorkbook.Saved = True

        Ret = MsgBox("Would you like to save this workbook?", vbYesNo)

        If Ret = vbYes Then SaveWithUniqueName
    End If
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If ThisWorkbook.Saved = True Then Exit Sub

    If SaveAsUI = True Then Exit Sub '~~> Checks for Save As

    Cancel = True
    SaveWithUniqueName
End Sub

Sub SaveWithUniqueName()
    Dim NewFileName As String

    On Error GoTo ERROR_HANDLER

        NewFileName = GenerateUniqueName(ThisWorkbook.FullName)

    If NewFileName <> "" Then
        Application.EnableEvents = False
        ThisWorkbook.SaveAs NewFileName, ThisWorkbook.FileFormat
        ThisWorkbook.Saved = True
        Application.EnableEvents = True
    End If

FastExit:
    On Error GoTo 0
    Exit Sub

ERROR_HANDLER:
    MsgBox "Error " & Err.Number & vbCr & _
        " (" & Err.Description & ") in procedure ThisWorkbook.Workbook_BeforeSave." & vbCr & vbCr & _
        "DOCUMENT NOT SAVED.", vbCritical + vbOKOnly
    Application.EnableEvents = True
    Resume FastExit
End Sub

答案 1 :(得分:0)

我稍微更新了我的BeforeSave代码 - 我仍然不确定ThisWorkbook.Saved = True : Cancel = True是否正确,但如果我没有放入Cancel = True,我确实知道它会崩溃:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim NewFileName As String

    On Error GoTo ERROR_HANDLER

    ThisWorkbook.Saved = True
    Cancel = True

    NewFileName = GenerateUniqueName(ThisWorkbook.FullName)
    If NewFileName <> "" Then
        Application.EnableEvents = False
        ThisWorkbook.SaveAs NewFileName, ThisWorkbook.FileFormat
        Application.EnableEvents = True
    End If

FastExit:

    On Error GoTo 0
    Exit Sub

ERROR_HANDLER:
    MsgBox "Error " & Err.Number & vbCr & _
        " (" & Err.Description & ") in procedure ThisWorkbook.Workbook_BeforeSave." & vbCr & vbCr & _
        "DOCUMENT NOT SAVED.", vbCritical + vbOKOnly
    Application.EnableEvents = True
    Resume FastExit

End Sub

这将使用新名称保存文件,但不会将其关闭。

正如苦艾酒和伯恩斯先生所说 - 看一下近距离事件 这样可以查看工作簿是否已保存。如果没有,则取消关闭事件,保存工作簿,然后关闭它,否则它将关闭而不保存。

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim NewFileName As String

    If Not ThisWorkbook.Saved Then
        Cancel = True
        NewFileName = GenerateUniqueName(ThisWorkbook.FullName)
        If NewFileName <> "" Then
            Application.EnableEvents = False
            ThisWorkbook.SaveAs NewFileName, ThisWorkbook.FileFormat
            Application.EnableEvents = True
            ThisWorkbook.Close Not ThisWorkbook.Saved
        End If
    End If

End Sub

有人能发现任何陷阱吗? 编辑:我发现了一个陷阱 - 您无法使用Save As