I found this code online that runs a Word Mail Merge and saves the separate documents in a folder. It works great. Then, it just stops saving the documents. I don't even get runtime errors.
The macro still generates the files, even now, but the save part doesn't work. When this happens, I have recreate everything from scratch. I have recreated this five times. It runs great, then it ..... Can you help me figure out what's going on?
Here's the code. I tweaked it to look for the folder on any user's desktop. Even without the tweak, I'm still experiencing problems after it runs a couple of times:
---------------
Sub GenerateDocuments()
'
'Start the mail merge
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
'Set the file location
Environ ("USERPROFILE") & "\Desktop\PAFMerge\"
'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
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
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="PAF_" & DocNum & ".doc"
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
'Prompt user when done
MsgBox "Your documents have been generated."
End Sub
----------------------------------
Thank you for your help.
The macro still generates the files, even now, but the save part doesn't work. When this happens, I have recreate everything from scratch. I have recreated this five times. It runs great, then it ..... Can you help me figure out what's going on?
Here's the code. I tweaked it to look for the folder on any user's desktop. Even without the tweak, I'm still experiencing problems after it runs a couple of times:
---------------
Sub GenerateDocuments()
'
'Start the mail merge
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection
'Set the file location
Environ ("USERPROFILE") & "\Desktop\PAFMerge\"
'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
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
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="PAF_" & DocNum & ".doc"
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
'Prompt user when done
MsgBox "Your documents have been generated."
End Sub
----------------------------------
Thank you for your help.
Last edited: