如何使用包含合并域的公式创建合并域
How to create a mergefield with a formula containing mergefields
我想构建合并域来决定来自两个不同合并域的数据。
例如 «field_1»
应该包含:
IF «field_1» > "" "«field_1»" "«field_2»"
我试过以下方法:
Sub createField()
Dim mergeString As 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
还有 insertFormula:
Sub createField()
Dim mergeString As String
mergeString = "IF{MERGEFIELD field_1}>"""" ""{MERGEFIELD field_1}""""{MERGEFIELD field_2}"""
Selection.InsertFormula Formula:= mergeString
End Sub
但这只是一团糟。
不幸的是,此代码只会将文本作为常规字符串而不是 Mergefields 插入。如果您在Word中手动插入字段,您可以使用Ctrl+F9键插入大括号来标记内容是一个字段而不是常规文本。
要将您想要的字段直接插入到 Word 中,您需要手动构建字段(使用 Ctrl+F9 并键入字段名称),或者您可以使用选项插入带占位符的 If 语句text 然后用所需的字段替换占位符。您需要按照相同的过程在 VBA.
中创建字段
子创建字段()
将 showState 调暗为布尔值
showState = ActiveWindow.View.ShowFieldCodes
Dim ifField As 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
With Selection.Find
.ClearFormatting
.Text = "FieldIfTrue"
.Replacement.Text = ""
.Forward = True
.Format = False
.Execute
End With
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField, Text:="""Name"""
With Selection.Find
.Text = "FieldIfFalse"
.Replacement.Text = ""
.Forward = True
.Format = False
.Execute
End With
ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="OtherName"
Selection.Fields.Update
ActiveWindow.View.ShowFieldCodes = showState
结束子
我想构建合并域来决定来自两个不同合并域的数据。
例如 «field_1»
应该包含:
IF «field_1» > "" "«field_1»" "«field_2»"
我试过以下方法:
Sub createField()
Dim mergeString As 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
还有 insertFormula:
Sub createField()
Dim mergeString As String
mergeString = "IF{MERGEFIELD field_1}>"""" ""{MERGEFIELD field_1}""""{MERGEFIELD field_2}"""
Selection.InsertFormula Formula:= mergeString
End Sub
但这只是一团糟。
不幸的是,此代码只会将文本作为常规字符串而不是 Mergefields 插入。如果您在Word中手动插入字段,您可以使用Ctrl+F9键插入大括号来标记内容是一个字段而不是常规文本。
要将您想要的字段直接插入到 Word 中,您需要手动构建字段(使用 Ctrl+F9 并键入字段名称),或者您可以使用选项插入带占位符的 If 语句text 然后用所需的字段替换占位符。您需要按照相同的过程在 VBA.
中创建字段子创建字段() 将 showState 调暗为布尔值 showState = ActiveWindow.View.ShowFieldCodes
Dim ifField As 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
With Selection.Find
.ClearFormatting
.Text = "FieldIfTrue"
.Replacement.Text = ""
.Forward = True
.Format = False
.Execute
End With
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField, Text:="""Name"""
With Selection.Find
.Text = "FieldIfFalse"
.Replacement.Text = ""
.Forward = True
.Format = False
.Execute
End With
ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="OtherName"
Selection.Fields.Update
ActiveWindow.View.ShowFieldCodes = showState
结束子