如何使用Word MailMerge将合并域插入VBA中的If..else语句?

时间:2018-04-12 20:00:59

标签: vba ms-word mailmerge mergefield

下面的宏将输入字段标题,后跟字段值。 If语句应该检查字段的值,如果它大于30,则给出实际值,否则为零。

如果它是真的,而不是出现的值,我只是得到我输入的实际文本,而不是它的值。

   Sub Macro1()
      Dim doc As Word.Document
      Dim dtField As Word.MailMergeDataField
      Dim sFieldName As String
      Dim sFieldActualName As String
      Dim j As Integer

      Set doc = ActiveDocument
      j = 1

      For Each dtField In doc.MailMerge.DataSource.DataFields

        sFieldActualName = doc.MailMerge.DataSource.FieldNames(j).Name
        sFieldName = dtField.Name
        Selection.TypeText Text:=sFieldActualName + ": "
'---------------------------------------------------------------------------
            doc.MailMerge.Fields.AddIf Range:=Selection.Range, MergeField:= _
                sFieldName, Comparison:=wdMergeIfGreaterThan,_
 CompareTo:="30", TrueText:="{MERGEFIELD sFieldName}", _
                FalseText:="0"
'---------------------------------------------------------------------------
        Selection.TypeParagraph
        j = j + 1

      Next

    End Sub

如果我需要澄清任何事情,请告诉我。

编辑:我在excel中使用的示例数据

one   two   three   four    five
85    50     63      50      41
52    10     84      10      15
85    25     63      35      10

1 个答案:

答案 0 :(得分:1)

据我所知,插入嵌套字段集的唯一方法是直接插入字段。嵌套字段很棘手 - 在互联网上有一些“外面”的方法。以下是我使用的那个。

在此变体中,最外面的字段插入了内部字段代码的占位符文本。占位符文本是带有类型括号的字段代码(不是Ctrl + F9类型)。

外部字段与占位符字符串一起发送到函数GenerateNestedField。该函数在字段的代码中定位占位符,并在其位置插入实际字段。

我必须修改我的标准代码才能处理为If字段插入MailMergeField的事实。有必要将MailMergeField转换为常规的Word.Field,我通过选择插入的字段,然后在Fields集合中获取第一个字段。

Sub IfPlusMergeField()
    Dim doc As word.Document
    Dim sFieldCode As String, sFieldName As String
    Dim fldMerge As word.MailMergeField
    Dim fldIf As word.Field

    Set doc = ActiveDocument
    sFieldName = dt.FieldName
    sFieldCode = "{Mergefield " & sFieldName & "}"
    Set fldMerge = doc.MailMerge.Fields.AddIf(Range:=Selection.Range, _
                   MERGEFIELD:=sFieldName, Comparison:=wdMergeIfGreaterThan, _
                   CompareTo:="30", TrueText:=sFieldCode, _
                   FalseText:="0")
    fldMerge.Select
    Set fldIf = Selection.Fields(1)
    Debug.Print GenerateNestedField(fldIf, sFieldCode)
End Sub

'Returns the changed field code
Function GenerateNestedField(fldOuter As word.Field, _
                             sPlaceholder As String) As String

    Dim rngFld As word.Range, doc As word.Document
    Dim bFound As Boolean
    Dim sFieldCode As String

    Set doc = fldOuter.Parent

    Set rngFld = fldOuter.code
    rngFld.TextRetrievalMode.IncludeFieldCodes = True
    bFound = rngFld.Find.Execute(findText:=sPlaceholder)
    'Get the field code from the placeholder by removing the { }
    sFieldCode = Mid(sPlaceholder, 2, Len(sPlaceholder) - 2)
    If bFound Then
        doc.Fields.Add rngFld, word.WdFieldType.wdFieldEmpty, sFieldCode, False
    End If
    'Debug.Print fldOuter.code

    GenerateNestedField = fldOuter.code
End Function