Mail Merge from Excel to Word

grapevine

Board Regular
Joined
May 23, 2007
Messages
208
I have been using Excel to instigate a mail merge for the last few years. I have just bought a new laptop and downloaded the files (Office 365 cloud based) and now the macros are playing up.
It appears to be the file path. I normally click in File Explorer to store the macro files on my pc but this is no different to before.

Can anyone throw any light on this?
Is there a versioning issue?
I have included the whole code but have highlighted the area where the code is failing. I am getting the message of Object not found. I have recopied the paths in case there were minor differences.
The same thing is happening on another macro as well and it is the file path that is causing the macro to fail so hopefully it is something simple that will rectify all the macros.
Any guidance gratefully received.


VBA Code:
Option Explicit
Dim FilePath As String
Dim coursedate As Date
Dim coursename As String
Dim companyname As String
Dim filename As String
Dim EmailSubject As String

'Dim fn As String
Dim email As String
Dim emailcc As String
Dim xOutlookObj As Object
Dim xEmailObj As Object
'Dim FilePath
Dim OApp As Object, OMail As Object
Dim signature As String
Dim Email_To As String
'Dim EmailSubject As String
Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
Dim PDFFILE As String

Sub CreateEvalsMailMerge()
coursedate = Range("f2")
coursename = Range("J2")
companyname = Range("H2")
'This is the path where your certificate is stored (the word document)
FilePath = "C:\Users\mkd\OneDrive - Grapevine Computing\Tutoring\Course Materials\Lesson Files\AuxcillaryDocs"

Call RunEvalsMerge
Call EmailEvalsPDF
Call MoveEvalsNames

End Sub
Sub RunEvalsMerge()

    Dim wd As Object
    Dim wdocSource As Object
    Dim objdoc As Word.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
'rename to the mail merge file you use)
[B] Set wdocSource = wd.Documents.Open(FilePath & "EvalsMM.docx")[/B]
    strWorkbookName = FilePath & 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 `Evals$`"

    With wdocSource.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute Pause:=False
    End With
Set objdoc = Word.Application.ActiveDocument

wdocSource.Close savechanges:=False

objdoc.Activate
    

objdoc.ExportAsFixedFormat OutputFileName:=FilePath & "Evaluations.pdf", _
ExportFormat:=wdExportFormatPDF
objdoc.Close savechanges:=False





End Sub

Sub EmailEvalsPDF()
'variables

EmailSubject = "Evaluations - " & coursedate 'Change this to change the subject of the email. The current month is added to end of subj line
OpenPDFAfterCreating = False 'Change this if you want to open the PDF after creating it : TRUE or FALSE
AlwaysOverwritePDF = False 'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE
DisplayEmail = True 'Change this if you don't want to display the email before sending.  Note, you must have a TO email address specified for this to work
'Email_To = ActiveSheet.Range("H2")  'Change this if you want to specify To email e.g. ActiveSheet.Range("H1") to get email from cell H1
'Email_CC = ActiveSheet.Range("I2")
PDFFILE = FilePath & "Evaluations.pdf"

    'Create an Outlook object and new mail message
Set OApp = CreateObject("Outlook.application")

Set OMail = OApp.CreateItem(0)


signature = OMail.Body
With OMail
'.To = Email
'.CC = emailcc
'        .BCC = Email_BCC
.Subject = EmailSubject
.Display
'.Body = "Please find attached invoice for work undertaken"
.Attachments.Add PDFFILE
'.Body = "Please find attached invoice for work undertaken" & vbNewLine & vbNewLine & signature
'.HTMLBody = "Please find attached certificates for recent course" & OMail.HTMLBody
.HTMLBody = "<BODY style=font-size:10pt font-family;calibri color:blue> Aram, <p> Please find attached Course Evaluations for " & coursename & " course run on the " & coursedate & " </p></Body>" & OMail.HTMLBody
End With
Set OMail = Nothing
Set OApp = Nothing
        
        
End Sub

Sub MoveEvalsNames()
Dim rw As Long
rw = Range("B" & Rows.Count).End(xlUp).Row
Sheets("Evals").Range("A2:W" & rw).Copy
Sheets("Evaluations").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
Sheets("Evals").Range("A2:W" & rw).Clear

Application.CutCopyMode = False

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Sorry the highlighting is not apparent apart from the /B, it is the line near the top
VBA Code:
 Set wdocSource = wd.Documents.Open(FilePath & "EvalsMM.docx")
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top