I wrote an Excel macro to send email from a spreadsheet. It works on Office 2013, but not Office 2016.
I looked at the VBA differences between Office 2013 and 2016, but couldn’t see anything about changes to the inspector or word editor for message objects.
Once it gets to
.GetInspector.WordEditor it throws:
Run-time error ‘287’:
Application-defined or object defined error
Here is the relevant part of the macro:
Sub SendEmail() Dim actSheet As Worksheet Set actSheet = ActiveSheet 'directories of attachment and email template Dim dirEmail as String, dirAttach As String ' Directory of email template as word document dirEmail = _ "Path_To_Word_Doc_Email_Body" ' Directories of attachments dirAttach = _ "Path_To_Attachment" ' Email Subject line Dim subjEmail As String subjEmail = "Email Subject" Dim wordApp As Word.Application Dim docEmail As Word.Document ' Opens email template and copies it Set wordApp = New Word.Application Set docEmail = wordApp.Documents.Open(dirEmail, ReadOnly:=True) docEmail.Content.Copy Dim OutApp As Outlook.Application Set OutApp = New Outlook.Application Dim OutMail As MailItem Dim outEdit As Word.Document ' The names/emails to send to Dim docName As String, sendEmail As String, ccEmail As String, siteName As String Dim corName As String Dim row As Integer For row = 2 To 20 sendName = actSheet.Cells(row, 1) sendEmail = actSheet.Cells(row, 2) ccEmail = actSheet.Cells(row, 3) siteName = actSheet.Cells(row, 4) Set OutMail = OutApp.CreateItem(olMailItem) With OutMail .SendUsingAccount = OutApp.Session.Accounts.Item(1) .To = sendEmail .CC = ccEmail .Subject = subjEmail & " (Site: " & siteName & ")" Set outEdit = .GetInspector.WordEditor outEdit.Content.Paste outEdit.Range(0).InsertBefore ("Dear " & sendName & "," & vbNewLine) .Attachments.Add dirAttach .Display '.Send End With Debug.Print row Set OutMail = Nothing Set outEdit = Nothing Next row docEmail.Close False wordApp.Quit End Sub
Things I’ve tried based on suggestions:
- Checked Outlook settings – default is HTML text
Ensure Word is the default email editor. From the Inspector.WordEditor dox:
WordEditorproperty is only valid if the
IsWordMailmethod returns True and the
olEditorWord. The returned
WordDocumentobject provides access to most of the Word object model…
Further, ensure that Outlook is configured to send Rich Text or HTML emails, not plain text.
I am not entirely sure if I had the same issue as you, but the call to
GetInspector started failing for me after upgrading Office 2016. So to be clear it worked with Office 2016 and then stopped working after the latest update.
The following workaround worked for me
dim item : set item = Addin.Outlook.CreateItemFromTemplate(Filename) Outlook.Inspectors.Add(item) ' Outlook is the application object
it only appears to work if I add the item straight after creating it, setting properties on it and then adding it did not work.
Note: I have not tested with
CreateItem instead of
CreateItemFromTemplate. The second line was added and unnecessary prior to the Office update.
Try moving the editor to the first action…
With OutMail Set outEdit = .GetInspector.WordEditor outEdit.Content.Paste .SendUsingAccount = OutApp.Session.Accounts.Item(1) .To = sendEmail .CC = ccEmail .Subject = subjEmail & " (Site: " & siteName & ")"