Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

792 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