Avatar of bukko
bukko

asked on 

Attached mail

Hi,

How do I open a mail item which is an attachment to another mail item?

We recieve orders by email. Each email has an attached text file containing customer details.
However, one customer is a subsidiary of a large US company which has a wierd mail checking system that attaches outgoing mail to another mail which it then sends on. So, instead of being attached to the mail we receive, the text file is attached to the attachment of the mail we receive.

This would be ok if we knew how to open the attached mail, but we don't.

I notice this question was asked some time ago with no success:
https://www.experts-exchange.com/questions/20077251/accessing-forwarded-message's-attachments.html?query=attached+mail+vb&clearTAFilter=true

Have there been any advances since?

Thanks...
Visual Basic Classic

Avatar of undefined
Last Comment
RomMod
Avatar of bukko
bukko

ASKER

If it helps, I have a feeling I need to use Outlook objects.
I think I might be able to save the attachment in an Outlook folder and then open it, but how?
Avatar of michaeldade
michaeldade

This is vbscrit to save attachments in outlook

For Each Atmt In Item.Attachments
             FileName = "C:\Email\Attachments\" & Atmt.FileName
             Atmt.SaveAsFile FileName
Next
Avatar of bukko
bukko

ASKER

Thanks.
How do I then open that attachment as an Outlook mail (if it IS a mail item)?

bukko
Avatar of bukko
bukko

ASKER

For example, do I need to save the attachment to an Outlook folder before I can open it?
Avatar of bukko
bukko

ASKER

...or can I save the file to a .pst file and import it?
Avatar of michaeldade
michaeldade

can you not save it as a .msg file (if it is a mail file) and open it directly
Avatar of bukko
bukko

ASKER

Yes, but I need to be able to open it programmatically and read the attached text file.

So for instance:

objAttachment.SaveAsFile "C:\...\mail.msg"

is not problem, and it open correctly when double-clicked, but how can I open it in VB (using Outlook objects or otherwise)?
Avatar of bukko
bukko

ASKER

(attached mail items are easy to recognise as the filename seems to always start with Attached, e.g. "Attached mail.msg")
Avatar of bukko
bukko

ASKER

Solved it.

You need to save the attached mail item to a file, (already knew that bit!) then open it as a template.
Avatar of bukko
bukko

ASKER

'Noddy' sample code as follows:

(note: folder C:\@atts must exist)

Option Explicit

Private Sub Form_Load()
    '
    Dim objApp As Outlook.Application
    Dim objItem As Outlook.MailItem
    Dim objAttachedItem As Outlook.MailItem
    Dim objAttachment As Outlook.Attachment
    Dim strText As String
    '
    Set objApp = New Outlook.Application
    For Each objItem In objApp.ActiveExplorer.CurrentFolder.Items
        For Each objAttachment In objItem.Attachments
            strText = GetTextFromAttachment(objAttachment)
            Stop ' DISPLAY TEXT AT THIS POINT !!!
        Next
    Next
    '
    Set objApp = Nothing
    '
End Sub

Private Function GetTextFromAttachment(objAttachment As Outlook.Attachment)
    '
    Dim strFilename As String
    Dim objMailItem As Outlook.MailItem
    '
    strFilename = objAttachment.FileName
    If Left(strFilename, 9) = "Attached " Then
        ' It's an attached email
        objAttachment.SaveAsFile "C:\@atts\" & objAttachment.Index & ".msg"
        Set objMailItem = objAttachment.Application.CreateItemFromTemplate("C:\@atts\" & objAttachment.Index & ".msg")
        GetTextFromAttachment = GetTextFromAttachment(objMailItem.Attachments(1))
    Else
        objAttachment.SaveAsFile "C:\@atts\" & objAttachment.Index & ".txt"
        GetTextFromAttachment = ReadFile("C:\@atts\" & objAttachment.Index & ".txt")
    End If
    '
End Function

Private Function ReadFile(strFilename As String) As String
    '
    Dim objFSO As Object
    Dim objFile As Object
    '
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFilename)
    ReadFile = objFile.ReadAll
    Set objFile = Nothing
    Set objFSO = Nothing
    '
End Function
ASKER CERTIFIED SOLUTION
Avatar of RomMod
RomMod

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo