Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error
I am trying to create ~80,000 separate word documents using a 5 page word template. When using mail merge it outputs one document, with the idea being that you will print this document and manually split these into the individual letters. For my purposes I need electronic copies. (my macro is listed below)
I am pulling catalog numbers and titles from an excel document for the mail merge. I mention this because having the resulting electronic copies named using the catalog number is critical to my project.
I have moved to running this macro on server as it takes a very long time to run.
I am running into two error codes:
Run-Time Error '5153' which seems to be related to an issue closing the current document. I am wondering how my code can be modified to override this and skip such documents.
and
run-time error '-2147221040 (800401d0)' dataobject:gettext openclipboard.
I am not quite sure what this one means other than an issue with the copy pastes.
I am a very new user of VBA and this Forum. Sorry if this is not formatted in the most logical way. Please let me know any thoughts you have or different approaches for solving the big picture here. Any help is greatly appreciated. I will be the first to admit that my code is not the most robust and has been stitched together with tidbits I find in various posts online.
My code:
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.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
'Collects the first line of the document to use in the save file
Selection.MoveUp Unit:=wdScreen, Count:=5
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=4, Extend:=wdExtend
Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious
Selection.Expand wdLine
Selection.Copy
'Deletes the first line so it doesn't show up in the PDF
'Stores the first line as an object to use in the save file
Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
Dim my_filename As String
my_filename = DataObj.GetText
ChangeFileOpenDirectory "F:\SRM\greg MSDS server folder"
DocNum = DocNum + 1
'Exports the Document as a PDF, with file name as the stored object, remember to change the output file name path to the same as your default from above, do this in the OutputFileName below
ActiveDocument.SaveAs FileName:=my_filename & ".doc"
ActiveDocument.Close
'Move the selection to the next section in the document
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
|