Outlook 2010 Macro to Convert Selected Email Messages to Plain Text

0

I need assistance creating a VBA Macro in Outlook 2010 that will convert one or many selected messages to Plain Text formatting. I am looking for a Macro instead of an incoming mail rule.

I found the following code that works as a rule, but in order for me to use it, I have to put the messages into a folder and run the rule manually:

http://www.outlookcode.com/article.aspx?id=62

Sub ConvertToPlain(MyMail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem

strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
objMail.BodyFormat = olFormatPlain
objMail.Save

Set objMail = Nothing
End Sub

I found some code a while ago that is able to remove all attachments from selected messages, that works perfectly for my needs, and if possible, I'm trying to duplicate it's functionality; however, instead of messing with attachments, I want it to convert the message to Plain Text.

http://www.slipstick.com/developer/code-samples/delete-attachments-messages/

Sub RemoveAttachments()
    Dim myAttachment        As Attachment
    Dim myAttachments       As Attachments
    Dim selItems            As Selection
    Dim myItem              As Object
    Dim lngAttachmentCount  As Long

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

    '  Loop though each item in the selection.
    For Each myItem In selItems
        Set myAttachments = myItem.Attachments

        lngAttachmentCount = myAttachments.Count

    ' Loop through attachments until attachment count = 0.
        While lngAttachmentCount > 0
            myAttachments(1).Delete
            lngAttachmentCount = myAttachments.Count
        Wend

        myItem.Save
    Next

    MsgBox "All Done. Attachments were removed.", vbOKOnly, "Message"

    Set myAttachment = Nothing
    Set myAttachments = Nothing
    Set selItems = Nothing
    Set myItem = Nothing
End Sub

My best effort at combining the 2 is as follows:

Sub ConvertPlainText()
    Dim selItems            As Selection
    Dim myItem              As Object
    Dim lngAttachmentCount  As Long
    Dim strID As String
    Dim objMail As Outlook.MailItem

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

    '  Loop though each item in the selection.
    For Each myItem In selItems
        Set myAttachments = myItem.Attachments

        lngAttachmentCount = myAttachments.Count

        strID = MyMail.EntryID
        Set objMail = Application.Session.GetItemFromID(strID)
        objMail.BodyFormat = olFormatPlain
        objMail.Save

        myItem.Save
    Next

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

    Set objMail = Nothing
    Set selItems = Nothing
    Set myItem = Nothing
End Sub

But I get an error that states "Object required" starting with the line:

strID = MyMail.EntryID

Any assistance would be greatly appreciated!!

Caleb

Posted 2016-08-29T17:51:27.513

Reputation: 23

Question was closed 2016-09-03T06:28:47.400

Hi! We're not a free script writing service, and just pasting code you found someplace else saying "I want to modify this to do X" does not count as the research we require for questions like this.. What have you tried already, and where are you getting stuck exactly while implementing your changes? – Ƭᴇcʜιᴇ007 – 2016-08-29T18:12:29.933

I've tried to combine the 2 codes, but when I go to execute it in Outlook, it states there's a Run-time error "Object required", and I'm not familiar enough with VBA to understand what I'm doing wrong. :-( – Caleb – 2016-08-29T19:46:28.903

1In your combined routine you haven't defined MyMail.EntryID. In your first routine MyMail is passed as a parameter ConvertToPlain(MyMail As MailItem). I don't know enough VBA to fix it, but that should give you enough of a clue to fix it yourself. – DavidPostill – 2016-08-30T08:47:39.647

Answers

0

There is a lot of extra code in your attempt to mimic RemoveAttachments.

Sub ConvertPlainText()

    Dim selItems            As Selection
    Dim myItem              As Object

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

    '  Loop through each item in the selection.
    For Each myItem In selItems
        myItem.BodyFormat = olFormatPlain
        myItem.Save
    Next

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

    Set selItems = Nothing

End Sub

Not in the question, but you may find EntryID is not needed in this code either.

Sub ConvertToPlain(MyMail As mailItem)
    MyMail.BodyFormat = olFormatPlain
    MyMail.Save
End Sub

niton

Posted 2016-08-29T17:51:27.513

Reputation: 1 724

Eureka!! Thank you all soo much! @DavidPostill, with your suggestion, I was able to change my code to work and when I came back to post my success, I compared my code to what niton suggested, and I had come to the same result!! (my code still had all the extra code, so thank you niton for cleaning it up!) Now it's time for me to get a book on VBA!! – Caleb – 2016-08-30T22:55:35.433