如何使用Word MailMerge在VBA中的If..else语句中插入合并字段? [英] How do I insert a mergefield into an If..else statement in VBA using Word MailMerge?

查看:310
本文介绍了如何使用Word MailMerge在VBA中的If..else语句中插入合并字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The below macro will input the field heading followed by the field value. The If statement should check the value of the field and if its greater than 30 the actual value is given, otherwise a zero.

我会得到输入的实际文本,而不是它的值,而不是显示为真的值.

Instead of the value appearing if it's true, I just get the actual text I've typed, not it's value.

   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

请让我知道是否需要澄清任何内容.

Please let me know if i need to clarify anything.

我正在使用excel中的示例数据

Sample data I'm using from excel

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

推荐答案

据我所知,插入嵌套字段集的唯一方法是直接插入字段.嵌套字段是棘手的-互联网上有两种方法在那儿".以下是我使用的一种.

As far as I know, the only way to insert a nested field set is to insert the fields directly. Nested fields are tricky - there are a couple of approaches "out there", in the internet. The following is the one I use.

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

In this variation, the outer-most field is inserted with placeholder text for the inner field code(s). The placeholder text is the field code with typed brackets (not the Ctrl+F9 kind).

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

The outer field is sent to the Function GenerateNestedField along with the placeholder string. The function locates the placeholder in the field's code and inserts the real field in its place.

我必须修改我的标准代码,以解决您要为If字段插入MailMergeField的事实.有必要将MailMergeField转换为常规的Word.Field,方法是选择插入的字段,然后将其取为Fields集合中的第一个Field.

I had to modify my standard code to work with the fact that you're inserting a MailMergeField for the If field. It's necessary to convert the MailMergeField to a regular Word.Field which I do by selecting the inserted field, then taking the first Field in the Fields collection.

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

这篇关于如何使用Word MailMerge在VBA中的If..else语句中插入合并字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆