Solved

Need to programmatically read the name of an Outlook Mail Attachment in VB or VBA

Posted on 2014-01-17
2
654 Views
Last Modified: 2014-01-20
I need to read a mail item attachment without first saving it to disk.

The code I am using is below.  This code will cycle through all the messages in my inbox and display the subject and body in the immediate window.  It will also allow me to cycle through the attachments and display the name of the attachments.

The problem is when a message bounces, the name handler attaches the original message to a new message as an attachment, and sends me the new message.

In order to programmatically determine what to do with the bounced message, I need to be able to read the body, or at least be able to read the subject.

In Outlook, if I click on the attachment it allows me to preview the message, so I'm sure this should be possible.

My code is below: This code will cycle through your inbox and display various info about each message in the immediate window.

I am using Outlook 2K

Sub TestMail()
Dim oM As MailItem
Dim oF As MAPIFolder
Dim olNS As outlook.NameSpace
Dim nn As Long
Dim NumberOfAttachments As Long
Dim pp As Long

Set olNS = Application.GetNamespace("MAPI")
Set oF = olNS.GetDefaultFolder(olFolderInbox)
 
 
 
 For nn = oF.Items.Count To 1 Step -1
                    Set oM = oF.Items(nn)
                    Debug.Print oM.Subject
                    Debug.Print oM.Body
                    Debug.Print oM.Attachments.Session
                    Debug.Print oM.Attachments.Parent

 NumberOfAttachments = oM.Attachments.Count

If NumberOfAttachments > 0 Then
    For pp = 1 To NumberOfAttachments
        Debug.Print oM.Attachments(pp).DisplayName

    Next pp
End If
                   
Next
End Sub
0
Comment
Question by:pcalabria
2 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39789165
Hi pcalabria,

What you can do is check to see if there is a message attached, and if so save it locally and then create a new message out of it. As far as I know there is no other way to see it as a message. You can then look at it like any other message.
In your "For pp" loop, insert the following:
        If oM.Attachments(pp).Type = olEmbeddeditem Then
         If Right(oM.Attachments(pp).FileName, 4) = ".msg" Then
          oM.Attachments(pp).SaveAsFile Environ("tmp") & "\tmp.msg"
          With Application.CreateItemFromTemplate(Environ("tmp") & "\tmp.msg")
           Debug.Print .Subject
           Debug.Print .Body
           .Delete
          End With
         End If
        End If

Open in new window

Matt
0
 

Author Closing Comment

by:pcalabria
ID: 39793828
Perfect solution!  Thanks for the code.
(Reading your code makes me realize how little I really know about programming!)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now