#1
|
|||
|
|||
I'm having a very difficult time trying to create a macro that uses "Select recipients" in Mailings then selects "Use an existing list" and then points to a certain directory.
I tried record macro but that gives me nothing. So in other words I want my users to be able to just select a button and it points to a directory that contains csv files to merge the template to. Whenever I assign this Macro to a Button in the QAT and I click it gives me "Argument Not Optional" However, If i run it in VBA it works. Code:
Sub Merge() ' ' Merge Macro ' ' Dialogs(wdDialogMailMergeOpenDataSource).Show With ActiveDocument.MailMerge .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With Documents("C:\Documents\mytemplate.docm").Close SaveChanges:=wdDoNotSaveChanges If Err.Number = 4160 Then MsgBox "The file specified is not open.", vbCritical Or vbOKOnly, _ "File Not Open" End If On Error GoTo 0 End Sub Thank you |
#2
|
||||
|
||||
The problem is the macro name which conflicts with Word built-in functions, call it e.g. MyMerge and you should be OK, though your error handling is a little off - try
Code:
Sub MyMerge() On Error GoTo err_Handler If Not ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument Then Dialogs(wdDialogMailMergeOpenDataSource).Show With ActiveDocument.MailMerge .Destination = wdSendToNewDocument .SuppressBlankLines = True With .DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .Execute Pause:=False End With Documents("C:\Documents\mytemplate.docm").Close SaveChanges:=wdDoNotSaveChanges Else MsgBox "Not a merge document" End If lbl_Exit: Exit Sub err_Handler: If Err.Number = 4160 Then MsgBox "The file specified is not open.", vbCritical Or vbOKOnly, _ "File Not Open" End If GoTo lbl_Exit End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Quote:
That worked! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Mail Merge is Deleting objects in my header and footer during the merge | bgranzow | Mail Merge | 9 | 06-05-2015 05:03 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 |
Macro to loop through all cells in a table produced by a mail merge | elh52 | Word VBA | 4 | 08-31-2014 10:50 PM |
Mail merge how to link mail merge field value to a column heading | dsummers | Mail Merge | 1 | 05-08-2014 02:59 PM |
Word Doc Macro (mail Merge) | ajolson1964 | Word VBA | 1 | 05-10-2011 10:15 PM |