Document.BeforeSave, événement (System 2007)

Mise à jour : Juillet 2008

Se produit avant l'enregistrement du document.

Espace de noms :  Microsoft.Office.Tools.Word
Assembly :  Microsoft.Office.Tools.Word.v9.0 (dans Microsoft.Office.Tools.Word.v9.0.dll)

Syntaxe

Public Event BeforeSave As SaveEventHandler

Dim instance As Document
Dim handler As SaveEventHandler

AddHandler instance.BeforeSave, handler
public event SaveEventHandler BeforeSave

Notes

Pour empêcher l'enregistrement du document, affectez la valeur true à l'argument Cancel de l'objet CancelEventArgs fourni.

Exemples

L'exemple de code suivant affiche un message qui vous demande, avant que le document soit enregistré, si vous souhaitez enregistrer le document.

Cette version est destinée à une personnalisation au niveau du document.

Private Sub DocumentBeforeSave()
    AddHandler Me.BeforeSave, AddressOf ThisDocument_BeforeSave
End Sub

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

private void DocumentBeforeSave()
{
    this.BeforeSave += new Microsoft.Office.Tools.Word.SaveEventHandler(ThisDocument_BeforeSave);
}

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

Cette version est destinée à un complément d'application.

Private Sub DocumentBeforeSave()
    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    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 = this.Application.ActiveDocument.GetVstoObject();
    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;
    }
}

Autorisations

Voir aussi

Référence

Document, classe

Membres Document

Microsoft.Office.Tools.Word, espace de noms

Historique des modifications

Date

Historique

Raison

Juillet 2008

Ajout d'une version de l'exemple de code pour un complément d'application.

Modifications de fonctionnalités dans le SP1.