共用方式為


Workbook.BeforeSave 事件 (Excel)

會在儲存活頁簿之前發生。

語法

運算式BeforeSave (SaveAsUICancel)

表達 代表 Workbook 物件的 變數。

參數

名稱 必要/選用 資料類型 描述
SaveAsUI 必要 布林值 True 是表示 如果顯示 [另 存新 檔] 對話方塊是因為變更需要儲存在活頁簿中。
Cancel 必要 布林值 False 事件發生時。 如果事件程序將這個引數設為 True,則該程序結束時,並不會儲存活頁簿。

傳回值

Nothing

範例

本範例會在儲存活頁簿之前,提示使用者是否要儲存活頁簿。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _ 
        Cancel as Boolean) 
    a = MsgBox("Do you really want to save the workbook?", vbYesNo) 
    If a = vbNo Then Cancel = True 
End Sub

此範例會使用 BeforeSave 事件來確認特定儲存格在儲存活頁簿之前包含資料。 除非以下各個儲存格均含有資料,否則無法儲存活頁簿:D5、D7、D9、D11、D13 及 D15。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
   'If the six specified cells don't contain data, then display a message box with an error
   'and cancel the attempt to save.
   If WorksheetFunction.CountA(Worksheets("Sheet1").Range("D5,D7,D9,D11,D13,D15")) < 6 Then
      MsgBox "Workbook will not be saved unless" & vbCrLf & _
      "All required fields have been filled in!"
      Cancel = True
   End If
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應