首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >丢失MS Word Mailmerge宏的格式

丢失MS Word Mailmerge宏的格式
EN

Stack Overflow用户
提问于 2013-02-01 17:49:18
回答 2查看 2.3K关注 0票数 1

使用MS Word 2010,我想要一个Mailmerge与宏一起运行,将每个记录保存为一个单独的文件,在PDF格式中使用其中一个字段作为文件名。这将为我节省大量的时间。

我遇到的问题是格式完全丢失了,就好像它只是复制文本并将其粘贴到新文档中一样。有没有什么办法可以保护格式,因为没有它是非常徒劳的…

提前谢谢。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-01 18:57:04

使用Save怎么样?

此示例代码遍历邮件合并文档中的每个邮件合并项目,将项目作为字母打开,并使用DataSource中的字段作为文件名将其保存到PDF。没有错误编码,也没有尝试检查重复的文件名。这是一个代码片段。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2013-12-06 20:16:47

首先,让我在值得称赞的地方给予肯定,因为我对编写宏一无所知。实际上,这是我第一次尝试使用宏,更不用说修改代码了。只有24年的基本知识(是的原始知识,不是Visual Basic)和Fortran (不是穿孔卡片格式,但真的很接近),我从http://raduner.ch/blog/microsoft-word-mail-merge-into-single-documents的Raduner先生的宏代码,Remou宏代码产生的pdf的上述,和其他几个,并结合不同的方面和快速!我显然非常幸运,但它在MS Word 2010中可以正常工作。希望它也适用于其他所有人。我正在加载个人pdf创建者和个人word文件创建者。我希望懂Visual Basic的人能把它弄清楚,使它对其他人更友好。

单个WORD文件宏(请注意,您的Excel数据源中必须有一个"FileName“列):

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14643524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档