共用方式為


XMLNode.ValidationError 事件

XMLNode 控制項出現驗證錯誤時便會發生。

命名空間:  Microsoft.Office.Tools.Word
組件:  Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)

語法

'宣告
Event ValidationError As EventHandler
event EventHandler ValidationError

範例

下列程式碼範例使用 SetValidationError 方法,設定 XMLNode 的自訂驗證錯誤訊息。 如果 XMLNode 不會進行驗證,則 ValidationError 事件的事件處理常式會顯示自訂驗證錯誤訊息。 這個範例假設目前文件包含兩個名為 CustomerAddress1Node 和 CustomerZipNode 的 XMLNode 物件,這兩個物件會對應到具有整數資料型別的結構描述項目。

Private Sub XMLNodeValidationError()

    ' Set custom error message for Address1 element.
    Dim errorText As String = Me.CustomerAddress1Node.BaseName & _
        " element must be an integer."
    Dim objErrorText As Object = CType(errorText, Object)
    Me.CustomerAddress1Node.SetValidationError( _
        Word.WdXMLValidationStatus.wdXMLValidationStatusCustom, _
        objErrorText, False)

    ' This does not raise a validation error.
    Dim val As Integer = 22222
    Me.CustomerZipNode.NodeText = val.ToString()

    ' This raises a validation error.
    Me.CustomerAddress1Node.NodeText = "Seventeen Hundred Twenty One"
End Sub

Private Sub CustomerNode_ValidationError(ByVal sender As Object, _
    ByVal e As EventArgs) Handles CustomerZipNode.ValidationError, _
    CustomerAddress1Node.ValidationError

    Dim tempNode As Microsoft.Office.Tools.Word.XMLNode = CType(sender,  _
        Microsoft.Office.Tools.Word.XMLNode)
    MsgBox("Error: " & tempNode.ValidationErrorText(False))
End Sub
private void XMLNodeValidationError()
{
    // Set custom error message for Address1 element.
    string errorText = this.CustomerAddress1Node.BaseName + 
        " element must be an integer.";
    object objErrorText = (object)errorText;
    this.CustomerAddress1Node.SetValidationError(
        Word.WdXMLValidationStatus.wdXMLValidationStatusCustom,
        ref objErrorText, false);

    // Attach validation event handlers.
    this.CustomerZipNode.ValidationError +=
        new EventHandler(CustomerNode_ValidationError);
    this.CustomerAddress1Node.ValidationError +=
        new EventHandler(CustomerNode_ValidationError);

    // This does not raise a validation error.
    int val = 22222;
    this.CustomerZipNode.NodeText = val.ToString();

    // This raises a validation error.
    this.CustomerAddress1Node.NodeText = 
        "Seventeen Hundred Twenty One";
}

void CustomerNode_ValidationError(object sender, EventArgs e)
{
    Microsoft.Office.Tools.Word.XMLNode tempNode = 
        (Microsoft.Office.Tools.Word.XMLNode)sender;

    MessageBox.Show("Error: " + tempNode.ValidationErrorText[false]);
}

.NET Framework 安全性

請參閱

參考

XMLNode 介面

Microsoft.Office.Tools.Word 命名空間