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!!
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 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. – DavidPostill – 2016-08-30T08:47:39.647