#1
|
|||
|
|||
5941 error message in macro used in merge
All, I have a mail merge and macro that has been running since 2003 that has all of a sudden stopped working with a 5941 error message. Word 2010. It debugs on the last line:
Windows("M_HERBARIUM_LABEL.DOTX").Activate Here is the whole code, any help greatly appreciated, thank you. Mike O. *** Sub MERGE_DEFAULT() ' ' MERGE_DEFAULT Macro ' Macro recorded 9/25/2003 by Greg Harmison ' With ActiveDocument.MailMerge .Destination = wdSendToNewDocument .MailAsAttachment = True .MailAddressFieldName = "" .MailSubject = "" .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With Application.Templates.LoadBuildingBlocks Windows("M_HERBARIUM_LABEL.DOTX").Activate End Sub |
#2
|
||||
|
||||
The error message is telling you that the file "M_HERBARIUM_LABEL.DOTX" isn't open.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks Paul, but I am running that macro from within the document itself, which is already open.
|
#4
|
||||
|
||||
The line:
Windows("M_HERBARIUM_LABEL.DOTX").Activate is trying to activate a the file named "M_HERBARIUM_LABEL.DOTX". By definition, any .DOTX file is a template, not a document. When you double-click on a template, Word creates a new document (typically named Document1 or something such). Furthermore, if the macro is in a template, that template cannot have a .DOTX extension, as the DOTX format does not support macros; a .DOT or .DOTM extension would be required. You say you've been using this macro since 2003, but the DOTX format did not exist back then - it was introduced with Word 2007. As for the code itself, it contains redundant lines that are only relevant for email merges and, do you realise the only (practical?) thing it does is to eliminate a few mouse clicks and switch back to the mailmerge main document after merging (so you then have to switch back to the output document before you can use it)? Finally, for the purposes of a mailmerge, you should be using a mailmerge main document, not a template. Since you're using macros, that document would have to be saved in the DOC or DOCM format. So as to not get tangled up with possible name changes to the mailmerge main document, you might change the code to: Code:
Sub MERGE_DEFAULT() Dim Doc As Document Set Doc = ActiveDocument With Doc.MailMerge .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False .Activate End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hello Paul - Greatly appreciate the response and on how to post code. More background on this - from our database application we are building a command line string that opens Word, opens a merge document and passes in a text file, runs the macros, and completes the merge. It really *has* worked since 2003, the code I posted contained my (bungled) attempts at fixing it recently.
In our string I have now changed it to a .DOTM file and made progress - I still receive the error message but the file *does* merge. But, each merged record is on it's own page, whereas previously we had six labels per page. (these are scientific labels for labeling botanical specimens, not mailing labels). When I tried the code you posted, with just .ACTIVATE, I get "Invalid or Unqualified Reference". Sounds like I need something in from of .ACTIVATE? Thank you again, very much. Mike O. |
#6
|
|||
|
|||
Also discovered if I remove .ACTIVATE from the last line the merge completes without an error message, but every record is still on a single page - looking to get back the functionality of multiple labels on a single page (see attached). Thank you for any continued help.
Mike O. |
#7
|
||||
|
||||
Actually, the code I posted should have been:
Code:
Sub MERGE_DEFAULT() Dim Doc As Document Set Doc = ActiveDocument With Doc With .MailMerge .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With .Activate End With End Sub Code:
Sub MERGE_DEFAULT() Dim Doc As Document Set Doc = ActiveDocument With Doc With .MailMerge .MainDocumentType = wdMailingLabels .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With .Activate End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Error 5941 when running my macro to auto populate fields throughout the word doc | VBAnovice1 | Word VBA | 2 | 05-26-2015 01:35 AM |
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error | zipit189 | Word VBA | 7 | 03-18-2015 01:13 PM |
Run-time error 5941 when deleting a row from a table | jpb103 | Word VBA | 1 | 05-26-2014 07:08 AM |
Mail Merge error message | rec | Mail Merge | 1 | 04-29-2011 10:30 PM |
Error Message in Mail Merge | A Bhavani | Mail Merge | 0 | 11-26-2008 12:08 PM |