XMLNode.SetValidationError(WdXMLValidationStatus, Object, Boolean) Method

Definition

Changes the validation error text displayed to a user for an XMLNode control and forces Microsoft Office Word to report a node as invalid.

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)

Parameters

Status
WdXMLValidationStatus

One of the WdXMLValidationStatus values that specifies whether to set the validation status error text (wdXMLValidationStatusCustom) or to clear the validation status error text (wdXMLValidationStatusOK).

ErrorText
Object

The text displayed to the user. Leave blank when the Status parameter is set to wdXMLValidationStatusOK.

ClearedAutomatically
Boolean

true to automatically clear the error message as soon as the next validation event occurs on the specified node. false to require running the SetValidationError(WdXMLValidationStatus, Object, Boolean) method with a Status parameter of wdXMLValidationStatusOK to clear the custom error text.

Examples

The following code example uses the SetValidationError method to set a custom validation error message for an XMLNode. An event handler for the ValidationError event displays the custom validation error message if the XMLNode does not validate. This example assumes that the current document contains two XMLNode objects named CustomerAddress1Node and CustomerZipNode that map to schema elements with an integer data type.

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

Remarks

To set custom error text, use the wdXMLValidationStatusCustom constant.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

Applies to