Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

vba - How can I convert a selection of emails from RichText to HTML Format?

Background: My work location has a default email format of RichText. Many people are adding images to their emails. RichText emails with images are much larger than HTML emails with images.

I can manually open, edit, changed format to HTML, and save to greatly reduce the size of the email. The formatting is maintained during conversion.

However, when I use VBA to open the mail item, change the format and save, the email is not converted.

How can I use vba to change a set of Richtext emails into properly formatted html emails, achieving the same results as manually editing and saving using the Ribbon?

Here is my sample code below, and I can see when I run the code that this line:

myMailItem.BodyFormat = olFormatHTML

is not converting in the same way that the Ribbon is converting.

Public Sub myConvertHTML()

    Dim mySelectedItems As Selection
    Dim myMailItem As MailItem
    Dim myRichText As String
    Dim myHtmlText As String


    ' Set reference to the Selection.
    Set mySelectedItems = ActiveExplorer.Selection

    ' Loop through each item in the selection.
    For Each myMailItem In mySelectedItems

        'if the current format is RichText proceed
        If myMailItem.BodyFormat = olFormatRichText Then

            myMailItem.Display

            'this line does not convert the RichText to Html properly
            'it seems to convert the images into an attached file ATT#### that is an ole object
            'instead of converting the image and using it in html format
            'when using the Ribbon and changing the format to HTML,
            'the email is converted and formatting maintained
            myMailItem.BodyFormat = olFormatHTML

            myMailItem.Save
            myMailItem.Close olSave


        Else
            MsgBox "Already in RichText Format: " & myMailItem.Subject, vbInformation

        End If

    Next

    MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"

    Set mySelectedItems = Nothing
    Set myMailItem = Nothing

End Sub

I tried manipulating the Ribbon from inside VBA, but I have not been able to figure out how to call the Ribbon element

(Format Text/Format/HTML).

Any suggestions are appreciated.


Thanks to Niton here is my working final solution.

Public Sub myConvertHTML002()
'takes selected list of items in Outlook and converts all mailItems that are RichText into HTML Format

    Dim mySelectedObjects As Selection
    Dim myObject As Object
    Dim myMailItem As MailItem

    Dim objItem As Object

    ' Set reference to the Selection.
    Set mySelectedObjects = ActiveExplorer.Selection

    ' Loop through each item in the selection.
    For Each myObject In mySelectedObjects

        If myObject.Class = olMail Then

            Set myMailItem = myObject

            'if the current format is RichText proceed
            If myMailItem.BodyFormat = olFormatRichText Then

                'have to display the email so we can use the command bars
                myMailItem.Display

                'special code because we can't change the format of the item from within the item
                'we have to use the ActiveInspector
                On Error Resume Next
                Set objItem = ActiveInspector.CurrentItem
                On Error GoTo 0

                If Not objItem Is Nothing Then
                    If objItem.Class = olMail Then
                        ActiveInspector.CommandBars.ExecuteMso ("EditMessage")
                        ActiveInspector.CommandBars.ExecuteMso ("MessageFormatHtml")
                    End If
                End If

                myMailItem.Close olSave

            Else

                'MsgBox "Already in RichText Format: " & myMailItem.Subject, vbInformation

            End If

        End If

    Next

    MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"

    Set mySelectedItems = Nothing
    Set myMailItem = Nothing

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can click a button with ExecuteMso.

CommandBars.ExecuteMso Method "Works on controls that are built-in buttons..."

Sub ChangeToHTML()
Dim objItem As Object
Dim objMail As mailitem

On Error Resume Next
Set objItem = ActiveInspector.currentItem
On Error GoTo 0

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

End Sub

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...