Hi. For some reason, there is no Post New Forum button even if I am logged in, so I used this thread to ask a question.
I have excel data with RESULT Tab to create a mail merge with output as individual Word template using a filename that is under column BO by just clicking a button I named 'AutoProduce'.
And this goes in loop.
It almost worked, but there were 2 problems. 1. Files produced were same data as per row 1. 2. Also, say, I have 4 records, one of the 4 records was named differently. Pls help me correct my code.
Thank you!
I have excel data with RESULT Tab to create a mail merge with output as individual Word template using a filename that is under column BO by just clicking a button I named 'AutoProduce'.
And this goes in loop.
It almost worked, but there were 2 problems. 1. Files produced were same data as per row 1. 2. Also, say, I have 4 records, one of the 4 records was named differently. Pls help me correct my code.
Code:
Private Sub CommandButton3_Click()
' merge1record_at_a_time Macro
Dim i As String
i = 1
Do While Worksheets("RESULT").Range("BQ2").Value <> ""
Dim templateType As String
templateType = Worksheets("RESULT").Range("AW1").Offset(i).Value
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
'Set specific path to get Confo Template
wdApp.Visible = True
'determine which template to use
If templateType = "dog" Then
Set wdDoc = wdApp.Documents.Open(FileName:="C:\animal Templates\dog.docx")
Else
Set wdDoc = wdApp.Documents.Open(FileName:="C:\animal Templates\cat.docx")
End If
'rem to change path
wdDoc.MailMerge.OpenDataSource Name:="C:\Macro\Animal Creation.xlsm" _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:="Provider=Microsoft.ACE.OLEDB.12.0;Source=C:\Macro\Animal Creation.xlsm;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Ty", _
SQLStatement:="SELECT * FROM `RESULT$`", SQLStatement1:=""
With wdDoc.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Dim DocName As String
DocName = Worksheets("RESULT").Range("BO1").Offset(i).Value
'Save the confirmation
'Remember to change Path
ChangeFileOpenDirectory "C:\Saved Templates"
ActiveDocument.SaveAs2 FileName:=DocName + ".docx", FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles:=True, ReadOnlyRecommended:=True, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
'Close it
wdDoc.Close False
i = i + 1
Loop
wdApp.Quit
MsgBox "All done.", vbInformation, "Auto-production completed."
End Sub
Thank you!
Last edited by a moderator: