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
 
thanks, I think I will work with WORD and then manually copy to email.

While searching, I came across THIS post. My suspision was corrent. It is a security issue as mentioned by jainashish.

As far as word editor is concerned, you do not need to manually copy it. You can use the MailItem.GetInspector property. You can use it like GetInspector.WordEditor to automaticaly get the data into your email without manually copying it. If you search the web there are lot of examples using GetInspector.WordEditor
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
While searching, I came across THIS post. My suspision was corrent. It is a security issue as mentioned by jainashish.

As far as word editor is concerned, you do not need to manually copy it. You can use the MailItem.GetInspector property. You can use it like GetInspector.WordEditor to automaticaly get the data into your email without manually copying it. If you search the web there are lot of examples using GetInspector.WordEditor

Thank you so much for your help, I'm kind of fresh for VBA,:LOL:, I don't understand too many methods, I'll try to work it out.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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