Opening Outlook Emails From Excel And Checking For Attachments\Embedded Items

JimHol

Active Member
Joined
Jan 4, 2011
Messages
314
Hi All!

I have an Excel that contains the drive locations of saved out Outlook emails. From here I need to open each email, check it for attachments, as well as, files embedded within the body of the email. I can do most of the code I just need help with the following:

1) Open the email located in "cells("A",2).value"

2) Check the email for file attachments.

3) Check the body of the email for embedded objects.

Any and all help greatly appreciated!

Thanks,
Jim
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this as a start. It works with Outlook message format files (.msg files):
Code:
Public Sub Outlook_Msg()

    Dim outApp As Outlook.Application
    Dim outEmail As Outlook.MailItem
    Dim outAttachment As Outlook.Attachment
    Dim saveInFolder As String
    
    saveInFolder = "C:\folder\path\"   'CHANGE THIS
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    On Error Resume Next
    Set outApp = GetObject(, "Outlook.Application")
    If outApp Is Nothing Then
        MsgBox "Outlook is not open"
        Exit Sub
    End If
    On Error GoTo 0
    
    Set outEmail = outApp.CreateItemFromTemplate(Range("A2").Value)
    
    For Each outAttachment In outEmail.Attachments
        outAttachment.SaveAsFile saveInFolder & outAttachment.Filename
    Next
    
End Sub
Needs a reference to MS Outlook Object library.
 
Upvote 0
John,

Thanks for your reply! I am getting a "Type Mismatch Error" on the "CreateItemFromTemplate" statement. Can you spot what I am doing wrong?

Code:
    Dim outApp As Outlook.Application
    Dim outEmail As Outlook.MailItem
    Dim outAttachment As Outlook.Attachment
    Dim saveInFolder As String
   
    Dim PresPath As String, relativePath As String, ThisFile As String

    Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, g As Long, h As Long, i As Long, jt As Long, x As Long, y As Long
        
    ThisFile = ThisRecord(0)
    a = ThisRecord(1)
    b = ThisRecord(2)
    c = ThisRecord(3)
    d = ThisRecord(4)
    e = ThisRecord(5)
    f = ThisRecord(6)
    g = ThisRecord(7)
    h = ThisRecord(8)
    i = ThisRecord(9)
    jt = ThisRecord(10)
    x = ThisRecord(11)
    y = ThisRecord(12)
    ThisEditSheet = ThisRecord(13)
    CellContents = ThisRecord(14)
    targetpath = ThisRecord(15)
    ProjectName = ThisRecord(16)
    DiscoveryJobName = ThisRecord(17)
    ThisTab = ThisRecord(18)
    ThisCheck = ThisRecord(19)
    
    saveInFolder = "C:\temp\"
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    
    Set outApp = GetObject(, "Outlook.Application")
    Set outEmail = outApp.CreateItemFromTemplate(ThisFile)
    
    NumberOfAttachments = outEmail.Attachments.Count
    
    For Each outAttachment In outEmail.Attachments
        outAttachment.SaveAsFile saveInFolder & outAttachment.Filename
    Next
 
Upvote 0
Try replacing that line with:
Code:
    Set outEmail = outApp.Session.OpenSharedItem(Range("A2").Value)
I've read that CreateItemFromTemplate opens a .msg file from disk in Outlook 2003, and OpenSharedItem works for 2007 (and higher?).
 
Upvote 0
John,

I am using Outlook 2007. I made the suggested changes but I am still getting a "Runtime Error 14 Type mismatch" on that line. Any ideas what maybe causing this problem? I do have the Microsoft Outlook Library referenced in VBA.

Thanks,
Jim
 
Upvote 0
What is in cell A2 of the active sheet? It should be the full path and file name of an Outlook .msg file, because you said:
1) Open the email located in "cells("A",2).value"
For example, C:\folder\path\An Outlook email.msg
 
Upvote 0
I actually pasted the full path directly into the code. If I past it into the run command the email does open fine.
 
Upvote 0
I am referring to my code - a simple test example.

For your code, what is the value of ThisFile at the CreateItemFromTemplate line (or the OpenSharedItem line)? From the code you've posted it appears to be empty (undefined), which explains the error.
 
Upvote 0
[TABLE="width: 145"]
<colgroup><col width="145"></colgroup><tbody>[TR]
[TD="class: xl63, width: 145"]Ok, I found the problem. It seams to be in the network path. The email below generates the error from the network location but works fine if i copy it to my local drive. Any ideas?

\\VECTOR03\ESI01\My Email\PR000004\Discovery Jobs\DJ000081\e-Mail\146000\145110\000000004A768C52CFFBC84D9AA3C7FC5E6328E084B63000.msg
[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I tried my code with a .msg file in a network location and it worked successfully. I don't know why yours produces that error.

One solution is for the code to first copy the .msg file from the network location to your local drive using the FileCopy statement, for example:
Code:
    Dim networkFile As String
    networkFile = "\\VECTOR03\ESI01\My Email\PR000004\Discovery Jobs\DJ000081\e-Mail\146000\145110\000000004A768C52CFFBC84D9AA3C7FC5E6328E084B63000.msg"
    FileCopy networkFile, ThisWorkbook.Path & Mid(networkFile, InStrRev(networkFile, "\"))
which copies the .msg file to the same folder as the workbook.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,693
Messages
6,173,868
Members
452,536
Latest member
Chiz511

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top