Question / Problem:
How do I create a script formatter with default initialization?
Answer / Solution:
The following script formatter initializes empty fields to "0.00" else it forwards the formatting task to the StandardAmountFormatter. This is especially useful for most of the additional amount fields (tax 2-4) which are in most cases not present on a Document, and should therefore be set to "0.00".
The same task could also be done in the Document_AfterExtract event, but then you need to repair all formatting flags so that the field is properly formatted.
Be aware that the invoice validation method requires that all input fields are formatted with an amount formatter. Using a script formatter makes this auto-initialization feature also available in Validation in case the user start entering values into an empty Document, e.g., after splitting a Document.
Private Sub EmptyAmounts_FormatDoubleField(ByVal FieldText As String, FormattedText As String, _ ErrDescription As String, ValidFormat As Boolean, _ ByRef DoubleVal As Double, _ ByRef DoubleFormatted As Boolean) Dim pfield As CscXDocField If Len(FieldText) = 0 Then ' mark the field as correctly formatted and assign the default text DoubleVal = 0.0 FormattedText = "0.00" DoubleFormatted = True ValidFormat = True Else ' create a temporary field and forward the formatting to the DefaultAmountFormatter ' make sure that there is an amount formatter with that name ' available in the current project Set pfield = New CscXDocField pfield.Text = FieldText ValidFormat = DefaultAmountFormatter.FormatField(pfield) FormattedText = pfield.Text ErrDescription = pfield.ErrorDescription DoubleVal = pfield.DoubleValue DoubleFormatted = pfield.DoubleFormatted End If End Sub