Vba 使用MS Word Mailmerge宏丢失格式
使用MS Word 2010,我希望邮件合并与宏一起运行,将每个记录保存为单独的文件,使用其中一个字段作为文件名,以PDF格式保存。这将节省我大量的时间 我遇到的问题是格式完全丢失了,就好像它只是复制文本并粘贴到新文档中一样。有没有什么方法可以保护格式,因为没有它是毫无结果的 提前谢谢Vba 使用MS Word Mailmerge宏丢失格式,vba,ms-word,ms-access-2010,mailmerge,Vba,Ms Word,Ms Access 2010,Mailmerge,使用MS Word 2010,我希望邮件合并与宏一起运行,将每个记录保存为单独的文件,使用其中一个字段作为文件名,以PDF格式保存。这将节省我大量的时间 我遇到的问题是格式完全丢失了,就好像它只是复制文本并粘贴到新文档中一样。有没有什么方法可以保护格式,因为没有它是毫无结果的 提前谢谢 Sub splitter() Dim i As Integer Dim Source As Document Dim Target As Document Dim Letter As Range Dim oFi
Sub splitter()
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
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
For i = 1 To ActiveDocument.MailMerge.DataSource.ActiveRecord
ActiveDocument.MailMerge.DataSource.ActiveRecord = i
Set Letter = Source.Range
For Each oField In Letter.Fields
If oField.Type = wdFieldMergeField Then
If InStr(oField.Code.Text, "INV_ID") > 0 Then
FileNum = oField.Result
End If
End If
Next oField
Set Target = Documents.Add
Target.Range = Letter
Target.SaveAs2 "C:\BACS\INVOICING\INVOICES\Word Export\" & FileNum, 17
Target.Close
Next i
End Sub
使用Save怎么样
此示例代码循环遍历mailmerge文档中的每个mailmerge项目,以字母形式打开该项目,并使用数据源中的字段作为文件名将其保存为PDF。没有错误编码,也没有尝试检查重复的文件名。这是一个片段
Dim iRec As Integer
Dim docMail As Document
Dim docLetters As Document
Set docMail = ActiveDocument
''There is a problem with the recordcount property returning -1
''http://msdn.microsoft.com/en-us/library/office/ff838901.aspx
docMail.MailMerge.DataSource.ActiveRecord = wdLastRecord
iRec = docMail.MailMerge.DataSource.ActiveRecord
docMail.MailMerge.DataSource.ActiveRecord = wdFirstRecord
For i = 1 To iRec
With docMail.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
'' This will be the file name
'' the test data source had unique surnames
'' in a field (column) called Surname
sFName = .DataFields("Surname").Value
End With
.Execute Pause:=False
Set docLetters = ActiveDocument
End With
docLetters.ExportAsFixedFormat OutputFileName:= _
"Z:\docs\" & sFName & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
docLetters.Close False
docMail.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next
首先,让我在值得赞扬的地方给予赞扬,因为我对编写宏一无所知。事实上,这是我第一次尝试使用宏,更不用说修改代码了。仅凭24年的基础知识(是的,不是VisualBasic)和Fortran(不是穿孔卡片Fortan,而是非常接近的),我就从Raduner先生那里获取了宏,删除了用于生成上述pdf的宏代码,以及其他一些代码,并将不同方面和PRESTO结合起来!!!我显然很幸运,但它在微软Word 2010中起作用。希望它也适用于其他人。我正在加载个人pdf创建者和个人word文件创建者。我希望了解VisualBasic的人能够清理这个问题,使它对其他人更加友好 单个WORD文件宏(注意,Excel数据源中必须有“文件名”列):
Sub SaveIndividualWordFiles()
Dim iRec As Integer
Dim docMail As Document
Dim docLetters As Document
Dim savePath As String
Set docMail = ActiveDocument
''There is a problem with the recordcount property returning -1
''http://msdn.microsoft.com/en-us/library/office/ff838901.aspx
savePath = ActiveDocument.Path & "\"
docMail.MailMerge.DataSource.ActiveRecord = wdLastRecord
iRec = docMail.MailMerge.DataSource.ActiveRecord
docMail.MailMerge.DataSource.ActiveRecord = wdFirstRecord
For i = 1 To iRec
With docMail.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
'' This will be the file name
'' the test data source had unique surnames
'' in a field (column) called FileName
sFName = .DataFields("FileName").Value
End With
.Execute Pause:=False
Set docLetters = ActiveDocument
End With
' Save generated document and close it after saving
docLetters.SaveAs FileName:=savePath & sFName
docLetters.Close False
docMail.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next
End Sub