XMLNode.SetValidationError 方法
變更 XMLNode 控制項顯示給使用者的驗證錯誤文字,強制 Microsoft Office Word 將節點回報為無效。
命名空間: Microsoft.Office.Tools.Word
組件: Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)
語法
'宣告
Sub SetValidationError ( _
Status As WdXMLValidationStatus, _
ByRef ErrorText As Object, _
ClearedAutomatically As Boolean _
)
void SetValidationError(
WdXMLValidationStatus Status,
ref Object ErrorText,
bool ClearedAutomatically
)
參數
- Status
型別:Microsoft.Office.Interop.Word.WdXMLValidationStatus
其中一個 WdXMLValidationStatus 值指定要設定驗證狀態錯誤文字 (wdXMLValidationStatusCustom) 還是要清除驗證狀態錯誤文字 (wdXMLValidationStatusOK)。
- ErrorText
型別:System.Object%
向使用者顯示的文字。當 Status 參數設定為 wdXMLValidationStatusOK 時留空白。
- ClearedAutomatically
型別:System.Boolean
true 表示在指定的節點上發生下一個驗證事件時,會立刻自動清除錯誤訊息。false 則表示要求使用 wdXMLValidationStatusOK 的 Status 參數執行 SetValidationError 方法,清除自訂錯誤文字。
備註
若要設定自訂錯誤文字,請使用 wdXMLValidationStatusCustom 常數。
選擇性參數
如需選擇性參數的詳細資訊,請參閱Office 方案中的選擇性參數。
範例
下列程式碼範例使用 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 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。