none
Outlook VBA to change message format if a certain email address is entered. RRS feed

  • Question

  • I am trying to set a macro for Outlook 2007/2010 that will change the message format to plaintext from html if a certain email address is entered in the To line. I also have it set up to automatically bcc any email to a given address.  The bcc portion works correctly but I cannot get the format code to work. Any help would be appreciated, the code is below. 

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    
    Dim objRecip As Recipient
    Dim strMsg As String
    Dim res As Integer
    Dim strBcc As String
    On Error Resume Next
    strBcc = "email@email.com"
    Set objRecip = Item.Recipients.Add(strBcc)
    objRecip.Type = olBCC
    If Not objRecip.Resolve Then
    strMsg = "Could not resolve the Bcc recipient. " & _
    "Do you want still to send the message?"
    res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
    "Could Not Resolve Bcc Recipient")
    If res = vbNo Then
    Cancel = True
    End If
    End If
    Set objRecip = Nothing
    
    End Sub
    
    
    Private Sub Application_NewMail()
    
    Dim objRecip As Recipient
    Dim strMsg As String
    Dim res As Integer
    On Error Resume Next
    If objRecip = "email@email.com"
        Set objMail.BodyFormat = olFormatPlain
        Else: Set objMail.BodyFormat = olFormatHTML
    End If
    
    End Sub
    


    Tuesday, August 27, 2013 11:37 AM

Answers

  • Firstly, you are resetting the body format of an incoming message, is that what you actually want?

    Secondly, do not use "set" with the BodyFormat property. It is not an object.

    Thirdly, why do you want to force HTML format for other recipients? What if the user decided to send in the plain text or RTF format? Would probably be a good idea to get rid of the "else" part in the "if" statement.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, August 27, 2013 4:47 PM

All replies

  • Have you tried to use MailItem.BodyFormat property?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, August 27, 2013 4:20 PM
  • Private Sub Application_NewMail() Dim objMail As MailItem Dim objRecip As Recipient If objRecip = "email@email.com" Then Set objMail.BodyFormat = olFormatPlain Else: objMail.BodyFormat = olFormatHTML End If End Sub

    I believe so i noticed some syntax error and changed the code to this. But when run I get an invalid use of property compiler error at the line "Set objMail.BodyFormat = olFormatPlain"
    Tuesday, August 27, 2013 4:41 PM
  • Firstly, you are resetting the body format of an incoming message, is that what you actually want?

    Secondly, do not use "set" with the BodyFormat property. It is not an object.

    Thirdly, why do you want to force HTML format for other recipients? What if the user decided to send in the plain text or RTF format? Would probably be a good idea to get rid of the "else" part in the "if" statement.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, August 27, 2013 4:47 PM
  • The purpose is set the format to Plain Text if a specific email is entered, and leave the formatting HTML if any other address is entered in the Recipient field.  
    Tuesday, August 27, 2013 4:49 PM
  • I think you are missing my point - you are not leaving the original message format as-is for other addresses. You are forcing it to be HTML.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Tuesday, August 27, 2013 4:52 PM