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.
Set FileDialogObject = Application.FileDialog(msoFileDialogFilePicker)
.Title = "Please select the template "
.InitialFileName = ThisWorkbook.Path & "\"
.AllowMultiSelect = False
.Filters.Add "Outlooktemplate Files", "*.oft,*.msg"
If .Show Then
Dim weeklyMSGName As String
weeklyMSGName = .SelectedItems.Item(1)
'Update the tempalte email
Dim OLKapp As Object
Set OLKapp = CreateObject("Outlook.Application")
Dim weeklyMSG As Outlook.MailItem
Set weeklyMSG = OLKapp.CreateItemFromTemplate(weeklyMSGName)
.Subject = Replace(.Subject, "Review", "Test") 'Works fine
.HTMLBody = Replace(.HTMLBody, "Index1", "Data1") 'Reporting error
Set weeklyMSG = Nothing
Set OLKapp = Nothing
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