vba - How to create a mergefield with a formula containing mergefields -
i want build mergefields decide between data coming 2 different mergefields. example «field_1»
should contain:
if «field_1» > "" "«field_1»" "«field_2»"
i tried following way:
sub createfield() dim mergestring string mergestring = "if{mergefield field_1}>"""" ""{mergefield field_1}""""{mergefield field_2}""" selection.fields.add range:=selection.range, type:=wdfieldempty, preserveformatting:=false selection.typetext text:=mergestring end sub
also insertformula:
sub createfield() dim mergestring string mergestring = "if{mergefield field_1}>"""" ""{mergefield field_1}""""{mergefield field_2}""" selection.insertformula formula:= mergestring end sub
but it's mess.
unfortunately, code insert text regular string rather mergefields. if insert fields in word manually, can use ctrl+f9 key insert braces mark contents field rather regular text.
to insert field want directly in word, either need construct fields manually (using ctrl+f9 , typing field names) or use option insert if statement placeholder text replace placeholders required fields. you'd need follow same process create field in vba.
sub createfield() dim showstate boolean showstate = activewindow.view.showfieldcodes
dim iffield mailmergefield set iffield = activedocument.mailmerge.fields.addif(range:=selection.range, mergefield:= _ "name", comparison:=wdmergeifnotequal, compareto:="", truetext:="fieldiftrue", _ falsetext:="fieldiffalse") iffield.select activewindow.view.showfieldcodes = true selection.find .clearformatting .text = "fieldiftrue" .replacement.text = "" .forward = true .format = false .execute end activedocument.fields.add range:=selection.range, type:=wdfieldmergefield, text:="""name""" selection.find .text = "fieldiffalse" .replacement.text = "" .forward = true .format = false .execute end activedocument.mailmerge.fields.add range:=selection.range, name:="othername" selection.fields.update activewindow.view.showfieldcodes = showstate
end sub
Comments
Post a Comment