Link to home
Start Free TrialLog in
Avatar of pcalabria
pcalabriaFlag for United States of America

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pcalabria

ASKER

Perfect solution!  Thanks for the code.
(Reading your code makes me realize how little I really know about programming!)