【发布时间】:2019-03-01 11:54:52
【问题描述】:
我正在使用以下宏将合并的邮件拆分为单独的文档。我需要的是拆分成单独的文档,保留整个页面,包括页眉和页脚,并保存为页面上的第一个合并字段,这是合并字母的第一条信息。
但是,宏只在一个字母上运行而不是其余的,格式完全不正确。它更改字体、页面布局并且不包括页眉和页脚。它还保存为“Ref”,而不是字母上的第一个合并字段。
有没有人知道如何修改下面的代码,以便正确更新上面的代码和所有字母?我知道这看起来是否真的很糟糕,但我是 VBA 的新手,我的项目中没有人寻求帮助。提前致谢
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 Ref 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, "Ref") > 0 Then
'get the result and store it the Ref variable
Ref = oField.Result
End If
End If
Next oField
Set Target = Documents.Add
Target.Range = Letter
Target.SaveAs FileName:="\\svr4958file01\Libraries\u20480\Documents\On Hold letters Template\20150512 On hold Letters Customers Active and Cancelled\" & "Ref"
Target.Close
Next i
End Sub
【问题讨论】:
标签: vba merge ms-word mailmerge