I am trying to take a selected message in Outlook, parse out the date it was received, the sender, the recipients and the subject line, and then create a file name with those elements, and then save it to a specified folder as a .msg file.
How can I parse out the information from a mail item to get the elements into my variables?
Sub Save_Message()
Set myOlApp = Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim mySender As Outlook.AddressEntry
Dim oMail As MailItem
Dim mSender As String
Dim mRecipient As String
Dim mDate As Date
Dim mSubject As Long
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
Set oMail = myOlSel.Item
mSender = oMail.SenderName
mRecipient = oMail.Recipients
mDate = oMail.ReceivedTime
mSubject = oMail.Subject
End Sub
Selectionis a collection. If you want the first element, useMailItem.Recipients returns Recipients collection, not a string. You can use
To/CC/BCCproperties (which are strings).Make sure you remove characters invalid in file name (such as
":"and";") when you construct the file name.As a general rule, please include the errors you are seeing when running your code.