This example demonstrates a table data check. The rule checks whether the table data matches the value in the resulting field. The table has columns with prices and quantities of certain goods. The rule has been written at the level of a section containing the table and the resulting field.
[VBScript]
dim autoRule
set autoRule = CreateObject( "AutomationRule.CheckDoc" )
autoRule.CheckResult me.FIELD("cost"), me.FIELD("number"), me.FIELD("Result"), me
[JScript]
var autoRule = new ActiveXObject( "AutomationRule.CheckDoc" );
autoRule.CheckResult( Field("cost"), Field("number"),
Field("Result"), this );
Code of an ActiveX component
The code provided here is a CheckDoc class code from the AutomationRule project, which is used in the scripts mentioned above.
Option Explicit
' Checks whether table data matches the result
' column1 - price column
' column2 - quantity column
' resultField - resulting field (<Total> or equivalent)
' docObj - document node being checked
Public Sub CheckResult(ByRef column1 As Object, ByRef column2 As Object, _
ByRef resultField As Object, ByRef docObj As Object)
On Error GoTo err_h
Dim costItems AsObject, numItems AsObject
Dim curCost AsDouble, curNum AsLong, sum AsDouble
Dim result AsDouble
Dim i AsLong
Set costItems = column1.Items
Set numItems = column2.Items
' summing up table data
sum = 0
For i = 0 To costItems.Count - 1
curCost = costItems.Item(i).Value
curNum = numItems.Item(i).Value
sum = sum + curNum * curCost
Next
' comparison accuracy result rounded to the 2nd digit after the decimal point
result = resultField.Value
If (Round(result, 2) = Round(sum, 2)) Then
docObj.CheckSucceeded = True
Else
docObj.CheckSucceeded = False
docObj.ErrorMessage = "Table data does not match the resulting value"
resultField.Suggest sum
End If
Exit Sub
err_h:
docObj.ErrorMessage = Err.Description
docObj.CheckSucceeded = False
End Sub