共用方式為


DocumentBase.XMLBeforeDelete Event

Occurs when a user deletes an XML element from a document. If more than one element is deleted from the document at the same time (for example, when cutting and pasting XML), the event is raised for each element that is deleted.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word.v4.0.Utilities (in Microsoft.Office.Tools.Word.v4.0.Utilities.dll)

Syntax

'Declaration
Public Event XMLBeforeDelete As DocumentEvents2_XMLBeforeDeleteEventHandler
public event DocumentEvents2_XMLBeforeDeleteEventHandler XMLBeforeDelete

Examples

The following code example demonstrates event handlers for the XMLAfterInsert and XMLBeforeDelete events. The code displays a message before an XMLNode is deleted from the document and after an XMLNode is added to the document. To test this code example, use the XML Structure task pane to add XML elements to the document, and then experiment with the Undo Typing and Redo Typing options on the Edit menu. This example assumes that the current document is mapped to a valid XML schema. To use this example, run it from the ThisDocument class in a document-level project.

Private Sub DocumentXMLBeforeAndAfterInsert()
    AddHandler Me.XMLAfterInsert, AddressOf ThisDocument_XMLAfterInsert
    AddHandler Me.XMLBeforeDelete, AddressOf ThisDocument_XMLBeforeDelete
End Sub  

Private Sub ThisDocument_XMLBeforeDelete(ByVal DeletedRange As Word.Range, ByVal OldXMLNode As Word.XMLNode, ByVal InUndoRedo As Boolean)
    If InUndoRedo Then
        MessageBox.Show(OldXMLNode.BaseName & " element is about to be deleted as a result" _
            & " of an undo or redo operation.")
    Else
        MessageBox.Show(OldXMLNode.BaseName & " element is about to be deleted.")
    End If 
End Sub 

Private Sub ThisDocument_XMLAfterInsert(ByVal NewXMLNode As Word.XMLNode, ByVal InUndoRedo As Boolean)
    If InUndoRedo Then
        MessageBox.Show(NewXMLNode.BaseName & " element was " & "inserted as a result " _
            & "of an undo or redo operation.")
    Else
        MessageBox.Show(NewXMLNode.BaseName & " element was inserted.")
    End If 
End Sub
private void DocumentXMLBeforeAndAfterInsert()
{
    this.XMLAfterInsert +=
        new Word.DocumentEvents2_XMLAfterInsertEventHandler(
        ThisDocument_XMLAfterInsert);

    this.XMLBeforeDelete +=
        new Word.DocumentEvents2_XMLBeforeDeleteEventHandler(
        ThisDocument_XMLBeforeDelete);
}

void ThisDocument_XMLBeforeDelete(Word.Range DeletedRange,
    Word.XMLNode OldXMLNode, bool InUndoRedo)
{
    if (InUndoRedo)
    {
        MessageBox.Show(OldXMLNode.BaseName +
            " element is about to be deleted as a result" +
            " of an undo or redo operation.");
    }
    else
    {
        MessageBox.Show(OldXMLNode.BaseName +
            " element is about to be deleted.");
    }
}

void ThisDocument_XMLAfterInsert(Word.XMLNode NewXMLNode,
    bool InUndoRedo)
{
    if (InUndoRedo)
    {
        MessageBox.Show(NewXMLNode.BaseName + " element was " +
            "inserted as a result of an undo or redo operation.");
    }
    else
    {
        MessageBox.Show(NewXMLNode.BaseName + " element was inserted.");
    }
}

.NET Framework Security

See Also

Reference

DocumentBase Class

Microsoft.Office.Tools.Word Namespace