从邮件合并中导出单个文档

问题描述 投票:0回答:4

如何从邮件合并创建个人文件,而不是由Microsoft Office中的邮件合并功能输出的一个大文件?

我希望能够保存每个字母,而不是创建一个合并字段的名称,但到目前为止我还没有找到一个直观的方法......

ms-word ms-office word-vba mailmerge
4个回答
2
投票

根据我的经验,没有保存单个文件的选项,而是可以使用宏来吐出文件并使用您想要的特定名称单独保存。我已经尝试过同样的想法并成功了。希望以下代码可以帮助您实现目标。

Sub BreakOnSection()
   'Used to set criteria for moving through the document by section.
   Application.Browser.Target = wdBrowseSection

   'A mailmerge document ends with a section break next page.
   'Subtracting one from the section count stop error message.
   For i = 1 To ((ActiveDocument.Sections.Count) - 1)

      'Select and copy the section text to the clipboard
      ActiveDocument.Bookmarks("\Section").Range.Copy

      'Create a new document to paste text from clipboard.
      Documents.Add
      'To save your document with the original formatting'
      Selection.PasteAndFormat (wdFormatOriginalFormatting)

      'Removes the break that is copied at the end of the section, if any.
      Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
      Selection.Delete Unit:=wdCharacter, Count:=1

      ChangeFileOpenDirectory "C:\"
      DocNum = DocNum + 1
      ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
      ActiveDocument.Close
      'Move the selection to the next section in the document
      Application.Browser.Next
   Next i
   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

如有任何澄清,请回复我。


3
投票

最近我遇到了类似的情况,我想以pdf格式保存单个文件,而不是保存由邮件合并功能创建的一个大文件。我写下了这个小函数来保存pdf格式的单个文件。

Sub SaveAsPDF()
Dim CouncilName  As String   
    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        For SectionCount = 1 To .DataSource.RecordCount
            With .DataSource
                'FirstRecord and LastRecords defines how many data records needs to be merge in one document.
                'createing pdf file for each data record so in this case they are both pointing to ActiveRecord.
                .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
                .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord

                'get the council name from data source 
                CouncilName = .DataFields("Council").Value  

                'move to next datasource record.
                If .ActiveRecord <> .RecordCount Then
                    .ActiveRecord = wdNextRecord
                End If
            End With

            'Get path and file name 
            PDFPathAndName = ActiveDocument.Path & Application.PathSeparator & "FINAL - " & CouncilName & ".pdf"

            ' Merge the document
            .Execute Pause:=False

            ' Save resulting document.
            Set PDFFile = ActiveDocument
            PDFFile.ExportAsFixedFormat PDFPathAndName, wdExportFormatPDF
            PDFFile.Close 0
        Next
    End With
End Sub

0
投票

试试这个 - 它对我有用 - 导出单词和pdf

http://www.gmayor.com/individual_merge_letters.htm


0
投票

我修改了Parth的答案,因为它对我不起作用。

Sub SaveAsFileName()
Dim FileName  As String
With ActiveDocument.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True

    For SectionCount = 1 To .DataSource.RecordCount
        With .DataSource
            ActiveDocument.MailMerge.DataSource.ActiveRecord = SectionCount
            ActiveDocument.MailMerge.DataSource.FirstRecord = SectionCount
            ActiveDocument.MailMerge.DataSource.LastRecord = SectionCount

            ' replace Filename with the column heading that you want to use - can't have certain symbols in the name
            FileName = .DataFields("Filename").Value
        End With

        'Get path and file name
        FullPathAndName = ActiveDocument.Path & Application.PathSeparator & FileName & ".docx"

        ' Merge the document
        .Execute Pause:=False

        ' Save resulting document.
        ActiveDocument.SaveAs (FullPathAndName)
        ActiveDocument.Close False
    Next
End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.