I have a spreadsheet containing names and courses which is connected to a Word Document which produces certificates and I want to automate the process.
I have managed to get the mail merge automated, but I cannot get the resulting Form Letter1 document to save as a PDF document.
I cannot seem to make the Form Letter 1 the document from which to create the pdf. My code is as follows: The part that is causing me problems is the section which I have highlighted separately - I have tried many different methods and just cannot seem to get this final part. Any better/easier solutions gratefully received or any pointers as to what I am doing wrong.
Many thanks
I have managed to get the mail merge automated, but I cannot get the resulting Form Letter1 document to save as a PDF document.
I cannot seem to make the Form Letter 1 the document from which to create the pdf. My code is as follows: The part that is causing me problems is the section which I have highlighted separately - I have tried many different methods and just cannot seem to get this final part. Any better/easier solutions gratefully received or any pointers as to what I am doing wrong.
Many thanks
Code:
Dim wrdDoc As Word.Document 'This is the part that is erroring out
Set wrdDoc = wd.Documents 'This is the part that is erroring out
Code:
Sub RunMerge()
Dim wd As Object
Dim wdocSource As Object
Dim objdoc As Document
Dim strWorkbookName As String
On Error Resume Next
Set wd = GetObject(, "Word.Application.16")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application.16")
End If
On Error GoTo 0
Set wdocSource = wd.Documents.Open("C:\Users\Marion\OneDrive - Grapevine Computing Ltd\Tutoring\Course Materials\Lesson Files\Auxcillary Documents\Certificate of Attendance.docx")
strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name
wdocSource.MailMerge.MainDocumentType = wdFormLetters
wdocSource.MailMerge.OpenDataSource _
Name:=strWorkbookName, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
SQLStatement:="SELECT * FROM `Certificates$` WHERE `Name` IS NOT NULL ORDER BY `Name` ASC "
With wdocSource.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
wd.Visible = True
wdocSource.Close SaveChanges:=False
Dim wrdDoc As Word.Document 'This is the part that is erroring out
Set wrdDoc = wd.Documents 'This is the part that is erroring out
wrdDoc.ExportAsFixedFormat OutputFileName:="C:\Users\Marion\OneDrive - Grapevine Computing Ltd\Tutoring\Course Materials\Lesson Files\Auxcillary Documents\Certificates.pdf", _
ExportFormat:=wdExportFormatPDF
End Sub