New Member
March 28, 2023
Hi, I'm new here. I have a problem when creating a new outlook email from a template(.oft or .msg), hope somebody can help.
The code as below, I don't know why the line ".HTMLBody = Replace(.HTMLBody, "Index1", "Data1")" always report an error 287, but the .subject can be replaced.
Does anybody know why that happens? Thanks a lot.
Sub MailTemplate()
Set FileDialogObject = Application.FileDialog(msoFileDialogFilePicker)
With FileDialogObject
.Title = "Please select the template "
.InitialFileName = ThisWorkbook.Path & "\"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Outlooktemplate Files", "*.oft,*.msg"
If .Show Then
Dim weeklyMSGName As String
weeklyMSGName = .SelectedItems.Item(1)
End If
End With
'Update the tempalte email
Dim OLKapp As Object
Set OLKapp = CreateObject("Outlook.Application")
Dim weeklyMSG As Outlook.MailItem
Set weeklyMSG = OLKapp.CreateItemFromTemplate(weeklyMSGName)
weeklyMSG.Display
With weeklyMSG
.Subject = Replace(.Subject, "Review", "Test") 'Works fine
.HTMLBody = Replace(.HTMLBody, "Index1", "Data1") 'Reporting error
End With
Set weeklyMSG = Nothing
Set OLKapp = Nothing
End Sub
Trusted Members
Moderators
November 1, 2018
Your code works fine here in Office 2016. Do you get the error with any template or just a specific one?
Also, you should learn the rules on cross-posting, as they are roughly the same on almost every forum. Xposted here: mrexcel.com/board/threads/vba-update-the-body-from-a-template-email.1233617
1 Guest(s)