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!!
MyMail.EntryID
. In your first routineMyMail
is passed as a parameterConvertToPlain(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.