Miserable Variable
Miserable Variable

Reputation: 28761

How to get the UI behavior when changing mail format to html?

The behavior when I change the format of a reply mail from Plain Text using Outlook VBA is not same as when using the Format Text/Format/HTML menu.

My original outgoing mail, just after hitting the Reply button looks as follows:

enter image description here

If I then click the HTML button it remains exactly same exactly same,

except for changing the format to HTML after which I can change the font, size etc.

But if I instead change the format in a macro using

...BodyFormat = olFormatHTML

(see complete code at bottom) then

enter image description here

Is there a way to get the UI behavior?

The macro body is as follows:

Sub ChangeToTextStyle()
Dim objItem As Object
Dim objMail As MailItem
On Error Resume Next

Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
    If objItem.Class = olMail Then
        Set objMail = objItem
        objMail.BodyFormat = olFormatHTML
    End If
End If
End Sub

UPDATE

Minor problems after following Eugene's answer:

Inserting objMail.Save before changing the format preserves the header and blank lines but the font changes to Times New Roman instead of Lucida Console, which is what I have set in options for composing and reading plain text mail. The original text shows up in 10pt and the cursor is at 12pt. How do I - Change font to Lucida Console 9.5pt for all the text in mail? - Change color at cursor to wdDarkRed?

Upvotes: 3

Views: 4092

Answers (4)

Fisher
Fisher

Reputation: 422

Below code works for me:

objItem.BodyFormat = olFormatHTML

Upvotes: 0

NanaB
NanaB

Reputation: 31

Otherwise you can just set the email into html format by replacing your code with the below:

Sub ChangeToTextStyle()
Dim objItem As Object

Dim objMail As MailItem
On Error Resume Next

Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
    If objItem.Class = olMail Then
        Set objMail = objItem
        .HTMLBody = .HTMLBody
    End If
End If
End Sub

That worked for me.

Upvotes: 0

niton
niton

Reputation: 9199

Try ExecuteMSO - http://msdn.microsoft.com/en-us/library/office/ff862419(v=office.15).aspx

"Works on controls that are built-in buttons..."

Sub ChangeToTextStyleHTML()
Dim objItem As Object
Dim objMail As mailitem
On Error Resume Next

Set objItem = Application.ActiveInspector.currentItem
If Not objItem Is Nothing Then
    If objItem.Class = olMail Then
        ActiveInspector.CommandBars.ExecuteMso ("MessageFormatHtml")
    End If
End If
End Sub

There are other ways but you can see MessageFormatHtml, the IdMso, as the last bit of text when you hover over the selection when adding a built-in button for the Quick Access Toolbar or a ribbon.

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

It seems you need to Save the mail item and close the inspector window to apply the changes to the UI. Outlook doesn't reflect changes immediately in the UI. Sometimes you need to re-open the message.

Upvotes: 1

Related Questions