Reputation: 49
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 I'm very new to VBA (I'm a C++ Programmer)
Upvotes: 1
Views: 654
Reputation: 49
So I found a solution to my problem.
I used the word macro recorder(View>Macros>Record Macro...) to record me entering the complex if statement, then viewed the macro, by pressing Alt + F11 and selecting macros, and I simply replaced some strings with my variables. and that has fixed the issues.
Hopefully this will be helpful for someone else, saving them hours of head scratching.
I had no Idea that you could record these actions to be reviewed later. You learn something new every day.
This is what the recording looked like.
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:=" = AND ("
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:=" COMPARE "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="MERGEFIELD FHB"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" = """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="T"
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.TypeText Text:=", "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="COMPARE "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="MERGEFIELD Other"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" <> """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="T"
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.TypeText Text:=")"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" = 1 """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="IfTrue"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="IfFalse"
Upvotes: 3