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(weeklyMSG.HTMLBody, "Index1", "Data1")" always report an error 287, but the .subject can be replace.
Does anybody know why that happens? Thanks a lot.
The code as below, I don't know why the line ".HTMLBody = Replace(weeklyMSG.HTMLBody, "Index1", "Data1")" always report an error 287, but the .subject can be replace.
Does anybody know why that happens? Thanks a lot.
VBA Code:
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
'Dim xbody As String
'xbody = weeklyMSG.HTMLBody
With weeklyMSG
.Subject = Replace(.Subject, "Review", "Test")
.HTMLBody = Replace(weeklyMSG.HTMLBody, "Index1", "Data1")
End With
Set weeklyMSG = Nothing
Set OLKapp = Nothing
End Sub