Accessories
Accessories

Reputation: 473

How to format outlook message which is formatted in PlainText

I have write below Outlook VBA Code to format Body Text.It works fine for HTML formatted message but its not working for "Plain Text" formatted message.Please check below code and advise how to do this...

    Public Sub FormatSelectedText()
 
    Dim objItem As Object
    Dim objInsp As Outlook.Inspector
    
    ' Add reference to Word library
    ' in VBA Editor, Tools, References
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection
    On Error Resume Next
   
'Reference the current Outlook item
    Set objItem = Application.ActiveInspector.CurrentItem
    If Not objItem Is Nothing Then
        If objItem.Class = olMail Then
            
            
            Set objInsp = objItem.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection

' replace the With block with your code
       With objSel
       ' Formatting code goes here
            .Font.Size = 12
            .Font.Italic = True
            .Font.Name = "Century Schoolbook"
            .Font.TextColor = RGB(31, 73, 125)
       End With

            End If
        End If
    End If
    objItem.BodyFormat = olFormatHTML
    objItem.Save
    
    Set objItem = Nothing
    Set objWord = Nothing
    Set objSel = Nothing
    Set objInsp = Nothing
End Sub

Upvotes: 0

Views: 2100

Answers (3)

Tragamor
Tragamor

Reputation: 3634

You may be interested in the code I use so that any reply/forwards etc of a message is converted to HTML (which is what I think you may actually be trying to do). You would need to edit the HTML style to what you want though...

In ThisOutlookSession

Private WithEvents olExplorer As Outlook.Explorer
Private WithEvents olItem As Outlook.MailItem

Private Const olReply As Integer = 1, olReplyAll As Integer = 2, olForward As Integer = 3

Private Sub Application_Startup()
    On Error Resume Next
    With Outlook.Application
        Set olExplorer = .ActiveExplorer
    End With
End Sub

Private Sub olExplorer_SelectionChange()
    On Error Resume Next
    Set olItem = olExplorer.Selection.Item(1)
End Sub

Private Sub olItem_Reply(ByVal Response As Object, Cancel As Boolean)
    On Error Resume Next
    If AlwaysReplyInHTML(olItem, olReply) = True Then Cancel = True
End Sub

Private Sub olItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    On Error Resume Next
    If AlwaysReplyInHTML(olItem, olReplyAll) = True Then Cancel = True
End Sub

Private Sub olItem_Forward(ByVal Response As Object, Cancel As Boolean)
    On Error Resume Next
    If AlwaysReplyInHTML(olItem, olForward) = True Then Cancel = True
End Sub

In a Module

Function AlwaysReplyInHTML(oItem As MailItem, Action As Integer) As Boolean
    AlwaysReplyInHTML = False

    'Change the message format if required and reply
    Dim oMsg As Outlook.MailItem: Set oMsg = oItem

    If oMsg.Class = olMail And oMsg.BodyFormat = olFormatPlain Then
        oMsg.BodyFormat = olFormatHTML

        Dim arr() As String: arr = Split(oMsg.HTMLBody, "<FONT SIZE=")
        For i = 1 To UBound(arr)
            arr(i) = Right(arr(i), Len(arr(i)) - 2)
        Next i
        oMsg.HTMLBody = Replace(Join(arr, ""), "<BODY>", "<BODY style=font-size:11pt;font-family:Calibri>")
        ' Any further processing

        Dim oMsgReply As Outlook.MailItem
        If Not oMsg.Sender Is Nothing Then
            Select Case Action
                Case 1: Set oMsgReply = oMsg.Reply
                Case 2: Set oMsgReply = oMsg.ReplyAll
                Case 3: Set oMsgReply = oMsg.Forward
            End Select
            oMsgReply.Display
            Set oMsgReply = Nothing
            AlwaysReplyInHTML = True
        End If
        oMsg.Close (olDiscard)
    ElseIf oMsg.Class = olMail Then
        ' Any further processing if e-mail already HTML
    End If
    Set oMsg = Nothing
End Function

Upvotes: 1

niton
niton

Reputation: 9199

You could use the code you have after changing the format to HTML.

Sub convertToHTML()

    Dim objItem As Object
    
    Set objItem = ActiveInspector.currentItem
    
    If objItem.Class = olMail Then
    
        With objItem
           If .BodyFormat <> olFormatHTML Then
                .BodyFormat = olFormatHTML
                .HTMLBody = .body
                .Save
           End If
        End With
        
    End If
End Sub

Upvotes: 0

braX
braX

Reputation: 11755

Using .HTMLBody you can make a simple "stylesheet" (css) for the fonts like this:

<style> * {font-size:12px; font-family:Century Schoolbook; color:#0000FF;} </style>

You can also use:

<style> * {font-size:12px !important; font-family:Century Schoolbook !important; color:#0000FF !important;} </style>

So to put it all together (instead of the way you are doing in your question, do it the way you did it in your question yesterday when I explained it)

Dim sStyleSheet As String
sStyleSheet = "<style> * {font-size:12px; font-family:Century Schoolbook; color:#0000FF;} </style>"

rest of your outlook object code here

.HTMLBody = sStyleSheet & "Here is my text"

more outlook code here

And do not remove that * as some people think they need to, as that's a wildcard that makes it work on all HTML elements.

If "it doesn't work" then you are doing something wrong when you put it all together.

Upvotes: 0

Related Questions