mardi 29 septembre 2015

Nested IF field in Word VBA

first timer here.

I have an issue that is causing me lots of grief. I'm trying to create a word addin that adds complex IF Statement Creation, from a list of possible mergefields.

Complex is

{ IF { = OR ( { COMPARE { MERGEFIELD Field_1 } <= "Value" }, { COMPARE { MERGEFIELD Field_2 } >= "Value" } ) } = 1 "True Instructions" "False Instructions" }

Im trying to do this all in VBA, but im having issues with my Complex if, as I cant get the "}" to end in the right locations.

If I use the terminator "Selection.EndKey Unit:=wdLine" in any other location besides the end, it creates a mess and putts all the } at that line.

Here is my Code:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:="IF "


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
    Selection.TypeText Text:=" = " & JointOperator1 & " ( "

'FIRST ARG
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
    Selection.TypeText Text:="COMPARE "


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="MERGEFIELD " & FirstArg1

    Selection.TypeText Text:=" "
    Selection.TypeText Text:=ComparisonType1
    Selection.TypeText Text:=" "
    Selection.TypeText Text:=Chr(34) & SecondArg1 & Chr(34)


Selection.TypeText Text:=", "


'SECOND ARG
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
    Selection.TypeText Text:="COMPARE "


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="MERGEFIELD " & FirstArg2

    Selection.TypeText Text:=" "
    Selection.TypeText Text:=ComparisonType2
    Selection.TypeText Text:=" "
    Selection.TypeText Text:=Chr(34) & SecondArg2 & Chr(34)


Selection.TypeText Text:=" ) "

Selection.TypeText Text:=" = 1 "

Selection.TypeText Text:=vbCrLf & " " & Chr(34)


Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:=strTempIfTrue


Selection.TypeText Text:=Chr(34) & " " & vbCrLf
Selection.TypeText Text:=" " & Chr(34)

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False
Selection.TypeText Text:=strTempIfFalse


Selection.TypeText Text:=Chr(34)
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph

And this is what I get when generating the "Complex if"

{IF { = AND ( {COMPARE{MERGEFIELD FHB} = "T", { COMPARE {MERGEFIELD BLAH} = "F") = 1 "If True text" "If False Text"}}}}

But it should be this:

{IF { = AND ( {COMPARE{MERGEFIELD FHB} = "T" } , { COMPARE {MERGEFIELD BLAH} = "F" } ) } = 1 "If True text" "If False Text"}

If someone could shed some light on this matter, where am I going wrong. Or If there is a way of forcing the location of the Ending }, that would be great Im very new to VBA (I'm a C++ Programmer)

Thank you very much in advance.

Aucun commentaire:

Enregistrer un commentaire