Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 06-02-2015, 08:52 AM
ch1325 ch1325 is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Jun 2015
Location: Montana
Posts: 12
ch1325 is on a distinguished road
Default

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
Any ideas or suggestions?

Thank you
Reply With Quote
  #2  
Old 06-05-2015, 05:31 AM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 2,705
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

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)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 06-08-2015, 06:18 AM
ch1325 ch1325 is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Jun 2015
Location: Montana
Posts: 12
ch1325 is on a distinguished road
Default

Quote:
The problem is the macro name which conflicts with Word built-in functions, call it e.g. MyMerge and you should be OK
Thanks for the reply,

That worked!
Reply With Quote
Reply

Thread Tools
Display Modes


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


All times are GMT -7. The time now is 07:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft