VBA MS Word-一次将所有邮件合并字段插入Word doc [英] VBA MS Word - Insert all mail merge fields into Word doc at once
问题描述
当前,MS Word仅允许一次插入1个邮件合并字段.我正在寻找一个VBA代码,该代码将允许我立即将所有合并字段插入到Word文档中和/或将要输入邮件合并字段的代码,其中相同邮件合并文本的名称将出现在文档中.>
对于第二种可能性,我发现了以下代码,该代码允许用户一次将与邮件合并名称匹配的文本转换为一个邮件合并字段( http://apsona.com/blog/how-to-quickly-create-merge-fields-in-word /).我正在处理一个包含数千个邮件合并字段的数据源,因此理想情况下,我想一次对所有这些合并字段进行创建,或者创建一个循环或类似的内容.
Sub MakeMergeField()
Selection.Copy
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD """ + Selection.Text + """, PreserveFormatting:=True"
End Sub
Word中没有功能可一次插入所有合并字段,因此您需要循环插入它们.
以下操作将每个合并字段分隔为一个空格.
(注意:通常,当您在Word中插入某些内容时,目标Range
会包含所插入的内容-合并字段并非如此.这就是为什么Range.Start
在每次插入后都移至文档末尾,然后再添加的原因空格.然后在下一次插入之前,必须Range
被折叠"到该空格之外.)
Sub InsertAllMergeFields()
Dim doc As word.Document
Dim rng As word.Range
Dim mm As word.MailMergeDataField
Set doc = ActiveDocument
Set rng = doc.content
If doc.MailMerge.MainDocumentType <> wdNotAMergeDocument Then
For Each mm In doc.MailMerge.DataSource.DataFields
doc.MailMerge.Fields.Add rng, mm.NAME
rng.Start = doc.content.End
rng.InsertAfter " "
rng.Collapse wdCollapseEnd
Next
End If
End Sub
Currently MS Word only allows for the insertion of 1 mail merge field at a time. I am looking for a VBA code that would allow me to insert all merge fields into a Word doc at once and/or a code that would input a mail merge field where the name of the same mail merge text appears in the doc.
For the second possibility I have found the following code that allows the user to convert text that matches a mail merge name into a mail merge field one at a time ( http://apsona.com/blog/how-to-quickly-create-merge-fields-in-word/ ). I am dealing with a data source that contains thousands of mail merge fields so ideally I would want to do this for all of them at once or create a loop or something similar.
Sub MakeMergeField()
Selection.Copy
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"MERGEFIELD """ + Selection.Text + """, PreserveFormatting:=True"
End Sub
There's no functionality in Word to insert all merge fields at once, so you need to insert them individually, in a loop.
The following does that, separating each merge field by a space.
(Note: Normally, when you insert something in Word, the target Range
encompasses what was inserted - not so with merge fields. That's why Range.Start
is moved to the end of the document after each insertion, before adding a space to it. Then the Range
needs to be "collapsed" beyond that space before doing the next insertion.)
Sub InsertAllMergeFields()
Dim doc As word.Document
Dim rng As word.Range
Dim mm As word.MailMergeDataField
Set doc = ActiveDocument
Set rng = doc.content
If doc.MailMerge.MainDocumentType <> wdNotAMergeDocument Then
For Each mm In doc.MailMerge.DataSource.DataFields
doc.MailMerge.Fields.Add rng, mm.NAME
rng.Start = doc.content.End
rng.InsertAfter " "
rng.Collapse wdCollapseEnd
Next
End If
End Sub
这篇关于VBA MS Word-一次将所有邮件合并字段插入Word doc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!