动态创建合并字段(访问 Word)

Dynamically create mergefields (Access to Word)

我是否可以使用数组值中的 VBA 设置 Word 文档中字段的文本?

Do While .Execute
        Selection.MoveDown Unit:=wdLine, count:=1
        Selection.Paragraphs.Indent

        For I = 0 To mergeFields.GetUpperBound(0)
            Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
            Selection.TypeText Text:="MERGEFIELD M_1"
            Selection.MoveRight Unit:=wdCharacter, count:=2
            Selection.TypeParagraph
        Next 

在 "MERGEFIELD M_1" 的位置,我想从名为 "mergeFields" 的数组中填充该文本,该数组具有类似命名的字符串。这样做的原因是因为该数组是在单独的函数中动态设置的,所以我可能需要创建 5 个新字段或最多 20 个新字段。

所以我的想法是:

            Selection.TypeText Text:= mergeField(I)
            Selection.MoveRight Unit:=wdCharacter, count:=2
            Selection.TypeParagraph

这样的事情可能吗?

没关系,经过更多调整后我能够让它工作。这是其他人的最终代码:

Do While .Execute
            Selection.MoveDown Unit:=wdLine, count:=1
            Selection.Paragraphs.Indent

            For Each fieldName In mergeFields
                If fieldName <> "" Then
                    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
                    Selection.TypeText Text:=mergeFields(I)
                    Selection.MoveRight Unit:=wdCharacter, count:=2
                    Selection.TypeParagraph
                    I = I + 1
                    End If
            Next
        Loop