Use the Word co-authoring features to collaborate on Word documents.
Recently I had to create a lot of documents based on the same template, so I decided to use. Unfortunately when you generate a document from a defined Mail Merge template in Word, you end up having all the pages inside one huge document. But I wanted to have each form in a seperate word document. To achieve this non-standard behavior, I wrote a little helper: execute the following VBA Macro on your Office Word Mail Merge template to have Word generate & save every record into a single file. Attention. Unfortunately this Macro does not work with Microsoft Office 2010+ on Windows!
(Reason is the next point #2). There is one thing to do manually, because I couldn’t solve it programmatically: you have to manually set the “Mail Merge Output” setting to “Current Record”! (default is “All”) Here’s the VBA code for the Macro: Alternative approach An is to split the generated mail merge document based on the section breaks that Word inserts when executing mail merging. For me this was not working because of special formatting and tables inside the tempalte document. 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 above, Remou macro code for producing pdf’s at the following link, and a few others and combined different aspects and PRESTO!!! I clearly got very lucky but it works in MS Word 2010. Hope it works for everyone else as well. I’m loading both individual pdf creator and individual word file creator.
Raduner will work his magic, clean this up and make it more user friendly for everyone else as he clearly knows way more than I do. I’ve just used your original code in Word2010, and many thanks for putting it together. I looked for quite a while for something clear and simple. While my last programming was in Basic (before QuickBasic even), I did manage a simple improvement which seems to avoid the whole “current record” issue.
You can leave it set to “All”. All of my individual documents appeared the same when opened, which forced me to try this: With ActiveDocument.MailMerge.DataSource.FirstRecord = rec.DataSource.lastRecord = rec.Destination = wdSendToNewDocument.Execute End With The DataSource.FirstRecord and lastRecord pointers (probably the wrong term) are all that I put in there, and now the rec variable insures that I get the right record. Works great now. The only other change I made was to hard-code the filepath since I wasn’t getting any kind of prompt. Anyway, maybe someone else can profit by this. Oliver, In other words, I used the code you show at the top as-is, with only the addition of two lines.
![Word For Mac Multiple Users Word For Mac Multiple Users](https://cdn2.macworld.co.uk/cmsdata/slideshow/3639907/libre_office_writer.png)
I’m running W7, Word 2010. Original: ‘ Execute Mail Merge action With ActiveDocument.MailMerge.Destination = wdSendToNewDocument.Execute End With Modified: ‘ Execute Mail Merge action With ActiveDocument.MailMerge.DataSource.FirstRecord = rec.DataSource.lastRecord = rec.Destination = wdSendToNewDocument.Execute End With I don’t think the problem you mention at the top is a problem anymore.
Can you verify? Hey tried the code you posted on December 6 for single PDF files. I cannot run it as it gives me an error in this part docLetters.ExportAsFixedFormat OutputFileName:= savePath & sFName & “.pdf”, ExportFormat:= wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=TrueCreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=TrueBitmapMissingFonts:=True, UseISO190051:=False docLetters.Close False I am using Windows for mac. Anyone an Idea? I am helpful for every hint. Cheers Giovanni. Thanks, this post was very helpful.
In my case i had to customise the macro script a little in order to save the documents as PDF’s instead. I changed two sections in the script as below: First replace both appearances of.docx with.pdf so the exported files have the correct file name extension: strDocName = “document” & rec & “.pdf” Else strDocName = ActiveDocument.MailMerge.DataSource.DataFields(docNameField).Value & “.pdf” Then add the bit that let’s Word know to export as a PDF: ActiveDocument.SaveAs FileName:=savePath & strDocName, FileFormat:=wdFormatPDF.
Hi, What do I change in the macro to save the file name using fields within the form? I read that I could either stipulate that using: 1.
The actual merge field within the document: and OR 2. The line on which the text sits on: so if my and field sits on the 3rd line of the document. Sub BreakOnSection ‘ Select a folder Dim strFolder As String Set fd = Application.FileDialog(msoFileDialogFolderPicker) With fd.Title = “Select the folder into which the documents will be saved.” If.Show = -1 Then strFolder =.SelectedItems(1) & “ ” Else MsgBox “The documents will be saved in the default document file location.” strFolder = “c: ” End If End With ChangeFileOpenDirectory strFolder ‘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.
![Word Word](https://cdn3.macworld.co.uk/cmsdata/slideshow/3639907/word_2016.png)
Documents.Add 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 DocNum = DocNum + 1 ‘ ActiveDocument.SaveAs FileName:=”test” & DocNum & “.doc” ActiveDocument.SaveAs ActiveDocument.Close ‘Move the selection to the next section in the document Application.Browser.Next Next i ActiveDocument.Close savechanges:=wdDoNotSaveChanges End Sub.