I am using multiple mail merge templates to create contracts in Microsoft Word. Right now I have 3 buttons with 3 different mail merge templates.
Is there a way to combine these 3 buttons into 1 so I can create 3 different contracts one after another without having to press the other 2 buttons?
When I try to combine all three using call I receive an error saying that the template can not be found. Is it because I have already a template open?
Note: I changed some of the file locations so dont worry if they look wrong
Sub MergeMe2()
Dim bCreatedWordInstance As Boolean
Dim objWord As Word.Application
Dim objMMMD As Word.Document
Dim cDir As String
Dim ThisFileName As String
Dim NewFileName As String
Const WTempName = "Contract Template.docx" 'This is the 07/10 Word Templates name, Change as req'd
cDir = "I:\Accounts Payable" 'Change if appropriate
ThisFileName = ThisWorkbook.Name
On Error Resume Next
bCreatedWordInstance = False
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Err.Clear
Set objWord = CreateObject("Word.Application")
bCreatedWordInstance = True
End If
If objWord Is Nothing Then
MsgBox "Could not start Word"
Err.Clear
On Error GoTo 0
Exit Sub
End If
On Error GoTo 0
objWord.Visible = True
Set objMMMD = objWord.Documents.Open("AccountsPayable.docx")
objMMMD.Activate
With objMMMD
.MailMerge.OpenDataSource Name:="I:\AccountsPayable.xlsm", sqlstatement:="SELECT * FROM `Export`"
With objMMMD.MailMerge 'With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = objMMMD.MailMerge.DataSource.ActiveRecord
.LastRecord = objMMMD.MailMerge.DataSource.LastRecord
End With
.Execute Pause:=False
End With
End With
objWord.ActiveDocument.SaveAs cDir + NewFileName
objMMMD.Close savechanges:=wdDoNotSaveChanges
Set objMMMD = Nothing
If bCreatedWordInstance Then
objWord.Quit
End If
0:
Set objWord = Nothing
End Sub
Bookmarks