Solved

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

Posted on 2014-01-17
2
659 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Read this checklist to learn more about the 15 things you should never include in an email signature.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

770 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