![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
You certainly picked a complex topic to begin your adventure into VBA
![]() You will find some code examples at http://www.gmayor.com/individual_merge_letters.htm as well as a process that will probably do what you want withgout any programming.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
Thank you for your reply.
I am working with the add-in on your site. I have placed MMtoDocs ver 4.6 into the start up folder per your website instructions. When I try to split my existing merge I receive the error "current document is not of the correct type for this function. It must be a Mail Merge Letter type document." I have tried saving this file in a variety of formats to no avail. Is there any guidance that you can offer me here? Thank you |
#4
|
||||
|
||||
![]()
The add-in does not split an existing document. It runs the merge process from the original merge document and data and creates new documents on the fly. To that end the document must be a letter merge document with an attached data file.
If you want to split an existing document, see the code samples on the linked web page.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
![]()
So the only way that I have been able to get this add in to function has been to place "MMtoDocs Ver 4.6" into the start up location you list on your website.
I have then opened that document and built my template within a copy of that document. Everything seemed to be working fine until I switched to a new template. The document I am working in is a "Macro-enabled word template". I am now receiving: "There has been an uncorrected error number: -2147467259" Any thoughts that you have on what I am doing wrong here is greatly appreciated ![]() I have read through your web instructions and am unable to troubleshoot. I feel like my creation of the template document is the issue but I dont really know. Thank you very much! |
#6
|
|||
|
|||
![]()
Some additional information:
I am only having this error when using the new template and it is creating the first document in my excel and producing an error afterwards. In case this is helpful. |
#7
|
||||
|
||||
![]()
MMtoDocs Ver 4.6 is an add-in. It goes in the Word startup folder as indicated in the instructions for use, where its processes become available to all documents.
It is not a document template and will cause conflict if you attempt to use it as such. Create your merge documents from another template, such as the normal template. Mailmerge with Word 2007/2010
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
|||
|
|||
![]()
So I think the error code was related to trying to run 2 of these simultaneously, one using a remote desktop connection to a server and another on my computer.
Since I have switched to one it appears to be functioning properly. Thank you for your help! I will let you know how this continues to progress. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Declan | Mail Merge | 1 | 05-28-2014 02:02 AM |
![]() |
tinfanide | Excel Programming | 3 | 10-01-2013 07:35 AM |
![]() |
BriMan83 | Mail Merge | 1 | 04-24-2013 11:35 PM |
Mail merge with filter using data inputted by user | Tornado70 | Mail Merge | 6 | 04-09-2013 07:16 AM |
![]() |
SaneMan | Mail Merge | 1 | 12-03-2011 01:11 AM |