VBA Update the body from a template email

hhj8810

New Member
Joined
Mar 28, 2023
Messages
13
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi,
Have you tested :
VBA Code:
With weeklyMSG
        .Subject = Replace(.Subject, "Review", "Test")
        .HTMLBody = Replace(.HTMLBody, "Index1", "Data1")
    End With
 
Upvote 0
Hi,
Have you tested :
VBA Code:
With weeklyMSG
        .Subject = Replace(.Subject, "Review", "Test")
        .HTMLBody = Replace(.HTMLBody, "Index1", "Data1")
    End With

Yes, I have tested :
.HTMLBody = Replace(.HTMLBody, "Index1", "Data1")
Actually .subject is only for comparison here, I don't need to change the subject, but I don't konw why it works for subject, while it's not working for .HTMLBody.

By the way, the codes are in excel, and I intend to use it to update data from excel sheet.
 
Upvote 0
Hi,
Have you tested :
VBA Code:
With weeklyMSG
        .Subject = Replace(.Subject, "Review", "Test")
        .HTMLBody = Replace(.HTMLBody, "Index1", "Data1")
    End With


when debug, the error always comes from this line. I see some examples using replace , I almost copied their codes, but I don't know why it does not work.


View attachment 88490
 
Upvote 0
Hi again,

For HTML, see if you can adapt following post :
 
Upvote 0
Hi again,

For HTML, see if you can adapt following post :
Hi, thanks for the reply. I don't thinks that will solve the problem. that's to define a string, and then make the body = string, i know the basic rules of HTML.
but my situation is that i have a temple which has the body, I don't need to define a new string, I just need to update the numbers in the body.
kina of like this:
 
Upvote 0
Hi again,

For HTML, see if you can adapt following post :
here's another example
 
Upvote 0
Error 287 is Application-defined or object-defined error.

Quick question... What happens when you use

VBA Code:
Replace(.Body, "Index1", "Data1")
 
Upvote 0
Error 287 is Application-defined or object-defined error.

Quick question... What happens when you use

VBA Code:
Replace(.Body, "Index1", "Data1")
Thanks for the reply.
Also error 287. I've tried several ways to fix this, and even tried define a new string like below, it reports error 287 too. so I cannot get the body out and edit it.
Dim xbody As String
xbody = weeklyMSG.HTMLBody
 
Upvote 0
In that case, it could be a security issue (I could be wrong about this). Which MS Office version are you using?
 
Upvote 0

Forum statistics

Threads
1,223,577
Messages
6,173,163
Members
452,503
Latest member
AM74

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top