Solved

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

Posted on 2014-01-17
2
669 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
[X]
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
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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