Nested IF field in Word VBA -
i'm trying create word addin adds complex if statement creation, 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 in vba, im having issues complex if, cant "}" end in right locations.
if use terminator "selection.endkey unit:=wdline" in other location besides end, creates mess , putts } @ line.
here 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 when generating "complex if"
{if { = , ( {compare{mergefield fhb} = "t", { compare {mergefield blah} = "f") = 1 "if true text" "if false text"}}}}
but should this:
{if { = , ( {compare{mergefield fhb} = "t" **}** , { compare {mergefield blah} = "f" **}** ) **}** = 1 "if true text" "if false text"}
if shed light on matter, going wrong. or if there way of forcing location of ending }, great i'm new vba (i'm c++ programmer)
so found solution problem.
i used word macro recorder(view>macros>record macro...) record me entering complex if statement, viewed macro, pressing alt + f11 , selecting macros, , replaced strings variables. , has fixed issues.
hopefully helpful else, saving them hours of head scratching.
i had no idea record these actions reviewed later. learn new every day.
this 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:=" = , (" 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"
Comments
Post a Comment