Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2015, 09:51 AM
zipit189 zipit189 is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2013
Novice
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error
 
Join Date: Mar 2015
Posts: 5
zipit189 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-13-2015, 03:45 AM
gmayor's Avatar
gmayor gmayor is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You certainly picked a complex topic to begin your adventure into VBA , and you really need to investigate the use of ranges rather than skipping about the document with selections.

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
Reply With Quote
  #3  
Old 03-13-2015, 12:50 PM
zipit189 zipit189 is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2013
Novice
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error
 
Join Date: Mar 2015
Posts: 5
zipit189 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 03-14-2015, 12:40 AM
gmayor's Avatar
gmayor gmayor is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 03-17-2015, 03:08 PM
zipit189 zipit189 is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2013
Novice
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error
 
Join Date: Mar 2015
Posts: 5
zipit189 is on a distinguished road
Default

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!
Reply With Quote
  #6  
Old 03-17-2015, 03:49 PM
zipit189 zipit189 is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2013
Novice
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error
 
Join Date: Mar 2015
Posts: 5
zipit189 is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 03-17-2015, 09:47 PM
gmayor's Avatar
gmayor gmayor is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #8  
Old 03-18-2015, 01:13 PM
zipit189 zipit189 is offline Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Windows 7 64bit Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Office 2013
Novice
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error
 
Join Date: Mar 2015
Posts: 5
zipit189 is on a distinguished road
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Split each page of mail merge into separate pdf Declan Mail Merge 1 05-28-2014 02:02 AM
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Run-time Error 5174: Open Word Documents from Excel tinfanide Excel Programming 3 10-01-2013 07:35 AM
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Split multi-page mail merge document, then name file from letter info. 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
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error Automatically split Mail Merge based on number of pages SaneMan Mail Merge 1 12-03-2011 01:11 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:47 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft