简体   繁体   English

MS Word Mailmerge宏丢失格式

[英]Losing format with MS Word Mailmerge Macro

Using MS Word 2010 I want a Mailmerge to run with a macro, saving each record out as a separate file, in PDF format using one of the fields as the filename. 使用MS Word 2010,我希望Mailmerge与宏一起运行,使用字段之一作为文件名将每个记录保存为单独的PDF格式文件。 This will save me loads of time. 这将节省我很多时间。

The problem i've got is that the format is being TOTALLY lost, as though it's just copying text and pasting it in a new document. 我遇到的问题是该格式已完全丢失,就好像它只是复制文本并将其粘贴到新文档中一样。 Is there any way I can protect the formatting as without it it's pretty fruitless... 我有什么办法可以保护格式,因为没有格式它是徒劳的...

Thanks in advance. 提前致谢。

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

How about using Save? 如何使用保存?

This sample code loops through each mailmerge item in a mailmerge document, opens the item as a letter and saves it to a PDF using a field in the DataSource as a file name. 此示例代码循环遍历mailmerge文档中的每个mailmerge项,以字母形式打开该项,然后使用DataSource中的字段作为文件名将其保存为PDF。 There is no error coding and no attempt to check for duplicate file names. 没有错误编码,也没有尝试检查重复的文件名。 It is a snippet. 这是一个片段。

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

First let me give credit where credit is due because I know absolutely nothing of writing macros. 首先,让我在应该归功的地方给予功劳,因为我完全不知道编写宏。 In fact this is my first attempt at using a macro let alone modifying the code. 实际上,这是我第一次尝试使用宏,更不用说修改代码了。 Armed only with 24 year old knowledge of Basic (yes the original, not Visual Basic) and Fortran (no not the punch card Fortan but really close) I took Mr. Raduner macro from http://raduner.ch/blog/microsoft-word-mail-merge-into-single-documents , Remou macro code for producing pdf's above, and a few others and combined different aspects and PRESTO!!! 我只具备24年的Basic基础知识(是原始知识,不是Visual Basic)和Fortran(不是Fortan打孔卡,但真的很亲密),我从http://raduner.ch/blog/microsoft-上带了Raduner先生宏将单词邮件合并到单个文档中 ,上面用于生成pdf的Remou宏代码,以及一些其他代码,并结合了不同方面和PRESTO! I clearly got very lucky but it works in MS Word 2010. Hope it works for everyone else as well. 我显然很幸运,但是它可以在MS Word 2010中使用。希望它也适用于其他所有人。 I'm loading both individual pdf creator and individual word file creator. 我正在加载单个pdf创建者和单个word文件创建者。 I hope someone that knows Visual Basic will clean this up and make it more user friendly for everyone else. 我希望知道Visual Basic的人能够解决此问题,并使它对其他所有人更加用户友好。

INDIVIDUAL WORD FILE MACRO (note you must have a "FileName" Column in your Excel data source): 单个单词文件宏(请注意,您在Excel数据源中必须具有“ FileName”列):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM