Bypass "Object Model Guard" when accessing a MailItem HTMLBody property

babibarbara

New Member
Joined
Jun 19, 2015
Messages
4
Hi, here is what I need: to create a rule that when a certain email arrives, I want to forward the message, but before I do I have to edit the body to delete the first 3 lines of the email, If I use the MailItem.Body it doesn't work because the email loses its formatting properties such as colors, images, etc, so I need to use the HTMLBody, my code works great, but everytime it gets to the MailItem.HTMLBody a question pops: "A program is trying to automatically send e-mail on your behalf. Do you want to allow this? If this is unexpected, it may be a virus and you should choose "No"."
How can I bypass this without using any add-ins, or third party programs? Only by vba code.
My code is in a module inside the outlook, I have a rule that call the macro "ChangeSubjectForward", and something as simple as this already triggers the message.

Sub ChangeSubjectForward(Item As Outlook.MailItem)
Dim htmlcode As String

htmlcode = Item.HTMLBody
End Sub

I'd be very happy if you guys can help me,
Thanks in advance.
Barbara
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Barbara

Can you post your current code?
 
Upvote 0
Barbara

Can you post your current code?

I have a rule in the Rules And Alerts funtion of Outlook 2003, that calls the ChangeAndForward function when I receive and email with the word "FORWARD:" in the subject. When someone send and email with this subject it also contains the line "To: email@email.com" on the first line of the email body. So what I need to do is grab the email to whom I will forward the message to, and delete this line with the email. Doing only modifying the Item.Body doesn't work because it loses all the images and other formats. So the idea is to use the Html code, which it works, but I keep getting the message I described before asking if I allow the access to the html code.

Code:
Sub ChangeAndForward(Item As Outlook.MailItem)
    Dim htmlcode As String
    Dim msgBody As Variant
    Dim line_to_remove As String
    Dim forward_to As String
    Dim forward_email As Outlook.MailItem
    
    msgBody = Split(Item.Body, vbCrLf & vbCrLf)
    line_to_remove = msgBody(0)
    forward_to = Trim(Split(line_to_remove, "To:")(1))
    htmlcode = Item.HTMLBody
    htmlcode = Replace(htmlcode, line_to_remove, "", 1, 1)

    Set forward_email = Item.Forward
    forward_email.HTMLBody = htmlcode
    forward_email.Recipients.Add (forward_to)
    forward_email.Send
End Sub
 
Last edited:
Upvote 0
How are you calling the sub?

I'm using the Oulook "Rules and Alerts" option, I created a rule that whenever an email with the word "FORWARD" in the email subject arrives it uses the option "Run a Script" and executes the macro "ChangeAndForward", then when this happens the outlook calls the function with the email as the Item parameter.
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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