我有一堆邮件合并模板设置,当我合并文档时,我想将结果拆分为单独的文件,每个文件都有一个基于合并字段“FileNumber”的名称。
我目前的代码是:
Sub splitter()
' Based on a macro by Doug Robbins to save each letter created by a mailmerge as a separate file.
' With help from http://www.productivitytalk.com/forums/topic/3927-visual-basic-question-for-merge-fields/
Dim i As Integer
Dim Source As Document
Dim Target As Document
Dim Letter As Range
Dim oField As Field
Dim FileNum As String
Set Source = ActiveDocument
For i = 1 To Source.Sections.Count
Set Letter = Source.Sections(i).Range
Letter.End = Letter.End - 1
For Each oField In Letter.Fields
If oField.Type = wdFieldMergeField Then
If InStr(oField.Code.Text, "FileNumber") > 0 Then
'get the result and store it the FileNum variable
FileNum = oField.Result
End If
End If
Next oField
Set Target = Documents.Add
Target.Range = Letter
Target.SaveAs FileName:="C:\Temp\Letter" & FileNum
Target.Close
Next i
End Sub
问题是如果我“合并到新文档”,那么“文件编号”字段不再存在,所以它无法选择,但如果我只是去“预览结果”并运行宏,它只会保存当前预览的记录和不是其余的字母。
我假设我需要将代码更改为类似
For i = 1 To Source.MergedRecord.Count
Set Letter = Source.MergedRecord(i).Range
但我无法找出正确的语法。
我知道http://www.gmayor.com/individual_merge_letters.htm但我不想要对话框,我只想要一个单击按钮。