Search code examples
vbams-wordfieldmergefield

Word Field "Syntax Error"


In a word file if have a field looking like:

{ = { IF "${COSTS}" = "" 0 "${COSTS}" } \# "#.0000" }

-> That gives my a Syntax Error!

However the following works fine:

{ = { IF "100" = "" 0 "100" } \# "#.0000" }

P.S: ${COSTS} is a placeholder that is replaced automatically by a software tool.

What can I do here?


Solution

  • The syntax of the IF field as described by the documentation is

    { IF Expression1 Operator Expression2 TrueText FalseText }
    

    In your first example the field value will be the text ${COSTS}, and not a number.

    The syntax of the outer formula field according to the documentation is

    { = Formula [Bookmark ] [\#Numeric Picture ] }
    

    The important part is the one about the formula (highlight by me):

    A formula is an expression that can contain any combination of numbers, bookmarks that refer to numbers, fields resulting in numbers, and the available operators and functions. The expression can refer to values in a table and values returned by functions.

    In your first example, the inner field does not yield a number, hence the syntax error.

    The solution will be to have the inner field return a number. Depending on where this number is coming from, this could e.g. be realized using bookmarks.