Forum

VBA Update the body...
 
Notifications
Clear all

VBA Update the body from a template email

2 Posts
2 Users
0 Reactions
279 Views
(@hhj8810)
Posts: 1
New Member
Topic starter
 

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

 
Posted : 29/03/2023 1:15 am
(@debaser)
Posts: 838
Member Moderator
 

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

 
Posted : 29/03/2023 4:23 am
Share: