none
Change Font in Outlook VBA

    Question

  • I am using VBA to process an incoming message based on a rule..

    Problem:

    Whenever I have a html text in the message it executes the script and displays it in HTML format

    Whenever I have a plain text in the message it executes the script and displays it in HTML format rather than displaying it in plain text format.

    So I want to change the font name to Consolas and font Size to 10.5 so that it will be virtually looking like plain text, but it is not actually plain text.I do not want to convert it as plain text because, my replaced hyperlinks will get lost.

     

    Question:

    How to change the font type in HTML inside a VBA code?

     

    My code for your reference

    Sub IncomingHyperlink(MyMail As MailItem)
    Dim strID As String
    Dim Body As String
    Dim objMail As Outlook.MailItem
    Dim RegX As Object
    
    strID = MyMail.EntryID
    Set objMail = Application.Session.GetItemFromID(strID)
      
      If objMail.BodyFormat = olFormatPlain Then
        Body = objMail.HTMLBody
         Set RegX = CreateObject("VBScript.RegExp")
        
      'Matching for the pattern
        With RegX
         .Pattern = "ASA[0-9][0-9][0-9][a-z][a-z]"
         .Global = True
         .IgnoreCase = Not MatchCase
        End With
    
        'Replacing the pattern with a hyperlink
        Body = RegX.Replace(Body, "<a href='http:www.msdn.com'>$&</a>")
        
      'How to change the font size and type here?
      
      objMail.HTMLBody = Body
      End If
    
    Set RegX = Nothing
    objMail.Save
    Set objMail = Nothing
    End Sub


    Thanks..

    Thursday, June 23, 2011 3:03 PM

Answers

  • Hi MacroOutlook,

     

    Do you try to handle your issue like this? I think this snippet will solve your issue.

     

    Body = RegX.Replace(Body,”<a href=’http:www.msdn.com’>$&</a>”)

    Body = <font size=”***”> & Body & “</font>”

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by MacroOutluk Wednesday, June 29, 2011 1:05 PM
    Tuesday, June 28, 2011 5:37 AM
    Moderator

All replies

  • Hi MacroOutluk,

     

    Thanks for posting in the MSDN Forum.

     

    According to your description, you want to change the font size of you link, is it right?

     

    If it is, I think the easiest way is use “ 

    Body = RegX.Replace(Body, "<font size=”**”><a href='http:www.msdn.com'>$&</a></font>")


    instead of

    Body = RegX.Replace(Body, "<a href='http:www.msdn.com'>$&</a>")

     

    I hope it can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, June 27, 2011 4:54 AM
    Moderator
  • Hi MacroOutluk,

     

    Thanks for posting in the MSDN Forum.

     

    According to your description, you want to change the font size of you link, is it right?

     

    If it is, I think the easiest way is use “ 

    Body = RegX.Replace(Body, "<font size=”**”><a href='http:www.msdn.com'>$&</a></font>")
    


    instead of

    Body = RegX.Replace(Body, "<a href='http:www.msdn.com'>$&</a>")
    

     

    I hope it can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thanks for posting your answer. But i want to change the font of the entire body whenever the email is composed in plain text format. How do I change the font of the entire email body in VB when using HTML body?

     

    Monday, June 27, 2011 3:48 PM
  • Hi MarcoOutlook,

     

    Thanks for posting in the MSDN Forum.

     

    What you mean of “entire body”? It’s based on my understanding that you want to change the whole mail body’s font style, is it right?

     

    I think you need show me more details about your goal, for example:

     

    1.       Is there use one font size in your mail item’s body, or different part will use different font size?

    2.       Do the Hyperlinks have underline?

    3.       Is there only have one paragraph? Or several paragraphs in your mail item’s body? Do them have different style?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 1:42 AM
    Moderator
  • Hi MarcoOutlook,

     

    Thanks for posting in the MSDN Forum.

     

    What you mean of “entire body”? It’s based on my understanding that you want to change the whole mail body’s font style, is it right?

     

    I think you need show me more details about your goal, for example:

     

    1.       Is there use one font size in your mail item’s body, or different part will use different font size?

    2.       Do the Hyperlinks have underline?

    3.       Is there only have one paragraph? Or several paragraphs in your mail item’s body? Do them have different style?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    The HTML body property of the mailitem body works good when the message is composed using HTML as the format.No question on this part.

    Suppose the message is composed using Plain Text Format, my macro displays it as HTML text after the entire processing has been completed. So I want to display this entire HTML text virtually as Plain Text with Font as Consolas and Font Size as 10.5. This will be looking like plain text, but it is not.

    the body will contain hyperlinks like ASA123ss with a underline on it.

    there will be several paragraphs, but no matter as we are going to change the font of the entire body after processing it using HTML property.

    Did I answer all your questions? if not please get back to me at the earliest...

    Thanks a lot...

    Tuesday, June 28, 2011 2:06 AM
  • Hi MacroOutlook,

     

    Do you try to handle your issue like this? I think this snippet will solve your issue.

     

    Body = RegX.Replace(Body,”<a href=’http:www.msdn.com’>$&</a>”)

    Body = <font size=”***”> & Body & “</font>”

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by MacroOutluk Wednesday, June 29, 2011 1:05 PM
    Tuesday, June 28, 2011 5:37 AM
    Moderator