【问题标题】:MS Word Mail Merge and Split Documents saving, Header and footer issueMS Word 邮件合并和拆分文档保存、页眉和页脚问题
【发布时间】: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


    【解决方案1】:

    为这个老问题提供一个替代答案,因为我最近不得不自己解决这个问题,并且在搜索这个问题时,这个问题仍然排名靠前。

    我从https://word.tips.net/T001538_Merging_to_Individual_Files.html 的宏开始,修改它以首先基于邮件合并文件创建单独的空白文档,以保留页眉、页脚和格式。这可能是一种低效的方法,但不需要乱用模板。

    下面的宏应该从需要拆分的邮件合并输出文档中运​​行。

    Sub BreakOnSection()
    
         '***Update the working folder location below***
         ChangeFileOpenDirectory "C:\C:\Users\User\Downloads"
    
         '***Update the original mail merge file name below***
         mailmergeoriginal = "Original Mail merge.docx"
    
        'Makes code faster and reduces screen flicker
        Application.ScreenUpdating = False
    
        'Used to set criteria for moving through the document by section.
        Application.Browser.Target = wdBrowseSection
        SectionCount = ActiveDocument.Sections.Count
    
        'Save a template for each mailmerge document
        ActiveDocument.StoryRanges(wdMainTextStory).Delete
        DocNum = 1
        For i = 1 To (SectionCount - 1)
            ActiveDocument.SaveAs FileName:="Mail merge " & DocNum & ".docx"
            DocNum = DocNum + 1
        Next i
    
        ActiveDocument.SaveAs FileName:="Macro temp.docx"
        Documents.Open FileName:= mailmergeoriginal
        Documents("Combined Offers.docx").Activate
    
        'A mailmerge document ends with a section break next page
        DocNum = 1
        For i = 1 To (SectionCount - 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.Open FileName:="Mail merge " & DocNum & ".docx"
            'To save your document with the original formatting'
            Selection.PasteAndFormat (wdFormatOriginalFormatting)
    
            'Removes any break copied at the end of the section
            Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
            Selection.Delete Unit:=wdCharacter, Count:=1
    
            ActiveDocument.SaveAs FileName:="Mail merge " & DocNum & ".docx"
            ActiveDocument.Close
            DocNum = DocNum + 1
    
            'Move the selection to the next section in the document
            Application.Browser.Next
        Next i
    End Sub
    

    请注意,此宏在运行后会留下一个额外的文件,名为“Macro temp.docx”,我需要保持打开以保持宏运行。完成后可以安全地删除此文件。这可能是可以避免的,但我想避免需要从模板运行宏并且没有想出更好的方法。

    【讨论】:

    • 谢谢!您知道如何将单个文件命名为名为“名称”的合并字段,而不是邮件合并 1、邮件合并 2 等吗?
    【解决方案2】:

    这只是对第二部分的回答:

    这一行:

    If InStr(oField.Code.Text, "Ref") > 0 Then
    

    正在查找其中包含“Ref”的合并字段。如果您需要不同的合并字段,则应将要保存文件的合并字段的名称放在“Ref”所在的位置,因此如果您的合并字段为“Addressee”,则将其更改为:

    If InStr(oField.Code.Text, "Address") > 0 Then
    

    此外,您的最后一行是使用字符串“Ref”而不是变量来保存文件名。您需要删除 Ref 周围的引号。它应该是:

    Target.SaveAs FileName:="\\svr4958file01\Libraries\u20480\Documents\On Hold letters Template\20150512 On hold Letters Customers Active and Cancelled\" & Ref
    

    至于其余部分,您可以使用另一种方法(我现在真的没有时间为此提供代码)。找到每个范围的第一页和最后一页(设置为变量 Letter)并将这些页面打印到 word doc。这将保留页眉和页脚。您需要输入的代码是:

    Letter.Information(wdActiveEndPageNumber) 
    

    获取范围末尾的页码(不确定,但我假设 (wdActiveStartPageNumber) 或类似的东西会获取范围的第一页

    Application.PrintOut From:=FirstPageNum, To:=LastPageNum, OutputFileName:=:="\\svr4958file01\Libraries\u20480\Documents\On Hold letters Template\20150512 On hold Letters Customers Active and Cancelled\" & Ref & ".doc"
    

    以后有时间会更新的。

    【讨论】:

    • 谢谢。好的,名称现在以正确的格式保存,但是我不确定如何将页眉和页脚添加到代码中,因为我不确定在哪里添加上述代码以及用什么替换它。请问你知道怎么处理吗?
    • 你需要做两个变量,FirstPageNum和LastPageNum。将它们设置为“字母”范围的第一页和最后一页。去掉“Set Target = Documents.Add”和下一行。然后使用我提供的“Application.PrintOut”行而不是 .SaveAs 行。
    猜你喜欢
    • 1970-01-01
    • 2022-12-03
    • 2013-03-09
    • 2017-05-26
    • 1970-01-01
    • 2021-07-21
    • 2015-04-03
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多