XMLNode.SetValidationError(WdXMLValidationStatus, Object, Boolean) 方法

定义

更改向用户显示的 XMLNode 控件的验证错误文本,并强制 Microsoft Office Word 将节点报告为无效。

public void SetValidationError (Microsoft.Office.Interop.Word.WdXMLValidationStatus Status, ref object ErrorText, bool ClearedAutomatically = true);
abstract member SetValidationError : Microsoft.Office.Interop.Word.WdXMLValidationStatus * obj * bool -> unit
Public Sub SetValidationError (Status As WdXMLValidationStatus, Optional ByRef ErrorText As Object, Optional ClearedAutomatically As Boolean = true)

参数

Status
WdXMLValidationStatus

WdXMLValidationStatus 值之一,该值指定是设置验证状态错误文本 (wdXMLValidationStatusCustom) 还是清除验证状态错误文本 (wdXMLValidationStatusOK)。

ErrorText
Object

显示给用户的文本。 将 参数设置为 wdXMLValidationStatusOK时留Status空。

ClearedAutomatically
Boolean

如果为 true,则在针对指定节点发生下一个验证事件后,立即自动清除错误消息。 false要求使用 Status 参数wdXMLValidationStatusOK运行 SetValidationError(WdXMLValidationStatus, Object, Boolean) 方法以清除自定义错误文本。

示例

下面的代码示例使用 SetValidationError 方法为 XMLNode设置自定义验证错误消息。 如果未XMLNode验证,事件的ValidationError事件处理程序将显示自定义验证错误消息。 此示例假定当前文档包含两 XMLNode 个名为 CustomerAddress1Node 的对象,这些 CustomerZipNode 对象映射到具有整数数据类型的架构元素。

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]);
}
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

注解

若要设置自定义错误文本,请使用 wdXMLValidationStatusCustom 常量。

可选参数

有关可选参数的信息,请参阅 Office 解决方案中的可选参数

适用于