Boris.S
Boris.S

Reputation: 1

Working with ".msg" Items in local folder

I'd like to get some attributes from emails stored in a local folder. I know how to do it for emails in Outlook.folder but I guess the methods are not the same in that case.

I can't find a way to work with the .msg items as if they were emails.

I haven't try anything since I understand the problem comes from object class incompatibility, but I don't know what to use. Also, I couldn't find a guide on ".msg" item, could be useful

Private Sub email_listing()
    On Error GoTo ErrHandler

    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")

    ' CREATE AND SET A NameSpace OBJECT.
    Dim objNSpace As Object
    ' THE GetNameSpace() METHOD WILL REPRESENT A SPECIFIED NAMESPACE.
    Set objNSpace = objOutlook.GetNamespace("MAPI")

    ' CREATE A FOLDER OBJECT.
    Dim myFolder, fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set myFolder = fs.GetFolder("C:\Users\boris\Desktop\Mail Test\")

    Dim Item As Object
    Dim iRows, iCols As Integer
    iRows = 2

    ' LOOP THROUGH EACH ITEM IN THE FOLDER.
    For Each Item In myFolder.Files
        If Item.Type = "Outlook Item" Then

            Dim objMail As Outlook.MailItem
            Set objMail = Item 'THE BUG

            Cells(iRows, 1) = objMail.SenderEmailAddress
            Cells(iRows, 2) = objMail.To
            Cells(iRows, 3) = objMail.Subject
            Cells(iRows, 4) = objMail.ReceivedTime
            Cells(iRows, 5) = objMail.Body
        End If

        iRows = iRows + 1
    Next
    Set objMail = Nothing

    ' RELEASE.
    Set objOutlook = Nothing
    Set objNSpace = Nothing
    Set myFolder = Nothing
ErrHandler:
    Debug.Print Err.Description
End Sub

I expect to have the attributes for each mail in my spreadsheet.

So far the code stop at line 27

Upvotes: 0

Views: 154

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

Use Namespace.OpenSharedItem to open standalone MSG files.

Upvotes: 0

Related Questions