Document.BeforeSave 事件

在保存文档之前发生。

命名空间:  Microsoft.Office.Tools.Word
程序集:  Microsoft.Office.Tools.Word(在 Microsoft.Office.Tools.Word.dll 中)

语法

声明
Event BeforeSave As SaveEventHandler
event SaveEventHandler BeforeSave

备注

若要防止保存文档,请将所提供的 CancelEventArgs 对象的 Cancel 参数设置为 true。

示例

下面的代码示例在您保存文档之前显示一条消息,询问您是否保存文档。 此示例针对的是应用程序级外接程序。

    Private Sub DocumentBeforeSave()
        Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
        AddHandler vstoDoc.BeforeSave, AddressOf ThisDocument_BeforeSave
    End Sub

    Private Sub ThisDocument_BeforeSave(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.SaveEventArgs)
        If System.Windows.Forms.MessageBox.Show( _
            "Do you want to save the document?", "BeforeSave", _
            System.Windows.Forms.MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

private void DocumentBeforeSave()
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.BeforeSave += new Microsoft.Office.Tools.Word.SaveEventHandler(ThisDocument_BeforeSave);
}

void ThisDocument_BeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e)
{
    if (System.Windows.Forms.MessageBox.Show("Do you want to save the document?", "BeforeSave",
        System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
    {
        e.Cancel = true;
    }
}

.NET Framework 安全性

请参见

参考

Document 接口

Microsoft.Office.Tools.Word 命名空间