Word 2007/2010 Mail Merge to save to individual PDF files

SuzeInk

New Member
Joined
Jan 30, 2014
Messages
1
I have a macro that works beautifully for splitting the merged doc into separate files. The issue is that these new files don't contain the page formatting of the original docs. It's a huge form very dependent on having the formatting exact. Any solutions for additions to the code to make this happen? The macro is below and came from this site. I am major newbie with macros so any help will be great.

Code:
Sub cdpdep()
'
' cdpdep Macro
'
'
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.AddSelection.Paste


        '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


Sub Macro1()
'
' Macro1 Macro
'
'
End Sub
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
The problem in this case is probably caused by a combination of your mailmerge main document not adhering to the:
A) page layout ; and
B) Style definitions,
used in the template upon which you're basing the output documents. These issues are exacerbated when the mailmerge main document doesn't even adhere to those parameters for its own template. You may be able to work arounf most of these issues by replacing:
Code:
        Documents.Add
        Selection.Paste

'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
with:
Code:
Dim wdDoc as Document
...
        DocNum = DocNum + 1
        Set wdDoc = Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=False)
        With wdDoc
            .Range.PasteAndFormat (wdFormatOriginalFormatting)
            While .Range.Characters.Last.Previous.Text = Chr(12)
                .Range.Characters.Last.Previous.Text = vbnullstring
            Wend
            .SaveAs .SaveAs fileName:="C:\test_" & DocNum & ".doc", AddToRecentFiles:=False
            .Close
        End With
 
Upvote 0

Forum statistics

Threads
1,225,665
Messages
6,186,312
Members
453,349
Latest member
neam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top