PDA

View Full Version : How to register individually a document and conserve layout (VBA)



joanie1234
11-15-2017, 12:37 PM
Hi,

I have a 2-page form to which I made a mail merge. (You'll find a example in attachment)


I would like to save it in Word individually, but I really have a problem with the code (and I'm more or less talented in this field)
1- The layout does not follow correctly

Here the code for now:

Sub BreakOnPage()
Application.Browser.Target = wdBrowseSection
For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
Dim mon_modele As String
mon_modele = ActiveDocument.AttachedTemplate.FullName

ActiveDocument.Bookmarks("\page").Range.Copy
Documents.Add Template:=mon_modele
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.PageSetup.Orientation = wdOrientLandscape

Selection.TypeBackspace
ChangeFileOpenDirectory "P:\ÉVÉNEMENTS SIGNATURES\2. Mois de la jonquille\Jonquilles 2018\4. Points de vente\Formulaire\Nouveau dossier\TEST"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="Formulaire jonquille_" & DocNum & ".docx"
ActiveDocument.Close

Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges

End Sub

Can someone help me with this?

Thank you so much! 2096420964

macropod
11-15-2017, 03:15 PM
See:
• Send Mailmerge Output to Individual Files; and
• Split Merged Output to Separate Documents,
in the Mailmerge Tips and Tricks thread at:
http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
or:
http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks

gmayor
11-15-2017, 10:08 PM
Your main problem is that you are trying to split your document by pages, when you need to split it by sections e.g.

Sub BreakOnSection()
Dim i As Integer, DocNum As Integer
Dim mon_modele As String
Application.Browser.Target = wdBrowseSection
For i = 1 To ActiveDocument.Sections.Count
mon_modele = ActiveDocument.AttachedTemplate.FullName

ActiveDocument.Sections(i).Range.Copy
Documents.Add Template:=mon_modele
Selection.PageSetup.Orientation = wdOrientLandscape
Selection.PasteAndFormat (wdFormatOriginalFormatting)

Selection.TypeBackspace
ChangeFileOpenDirectory "P:\ÉVÉNEMENTS SIGNATURES\2. Mois de la jonquille\Jonquilles 2018\4. Points de vente\Formulaire\Nouveau dossier\TEST"
DocNum = DocNum + 1
ActiveDocument.SaveAs Filename:="Formulaire jonquille_" & DocNum & ".docx"
ActiveDocument.Close

Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub