Solved

Extract Attachments from .msg messages but exclude embedded

Posted on 2013-12-08
1
549 Views
Last Modified: 2013-12-17
Hello,

I have the first draft of a vba macro but while testing i am noticing that the extraction part of this code also extracts embedded images from the email and not just the attached files.  can anyone help me to extract only the attached messages and not embedded images?


Sub Extract_Attachments()
    Dim myDir As String, myFile As String
    Dim newDir As String
    Dim count As Integer
    Dim logfile As String
    Set FSO = CreateObject("Scripting.FileSystemObject")

    'your folder with msg files here
    myDir = "c:\Burn"
    'folder for your attachments
    newDir = "c:\Burn"
    'log file
    logfile = myDir & "\log.txt"

    If FileExists(logfile) Then
      SetAttr logfile, vbNormal
      Kill logfile
    End If
   
    Set ofile = FSO.CreateTextFile(logfile)
    ofile.writeline "List of files with NO attachments"
 
    myFile = Dir(myDir & "\*.msg")
    'Application.ScreenUpdating = False
    Do While myFile <> ""
        If Not GetMsg(myDir & "\" & myFile, newDir) Then
            ofile.writeline "-->" & myFile
        End If
        myFile = Dir
    Loop
    ofile.Close
    Shell "notepad.exe " & logfile, vbNormalFocus ' open a txt document
    Shell "explorer.exe " & newDir, vbNormalFocus
End Sub
 
Function FileExists(ByVal FileToTest As String) As Boolean
   FileExists = (Dir(FileToTest) <> "")
End Function
 
Function GetMsg(ByVal OlFilePath As String, ByVal NewFilPath As String) As Boolean
    Dim oLapp, oMsg, olAtt
    Set oLapp = CreateObject("outlook.application")
    Set oMsg = oLapp.CreateItemFromTemplate(OlFilePath)
    GetMsg = False
    Dim count As Integer
    count = 0
   
    For Each olAtt In oMsg.Attachments
        olAtt.SaveAsFile NewFilPath & "\" & Left(Replace(OlFilePath, NewFilPath + "\", ""), 3) & "-" & olAtt.FileName
        count = count + 1
    Next
   
    If count > 0 Then
        GetMsg = True
    End If
    Set olAtt = Nothing
    Set oMsg = Nothing
    Set oLapp = Nothing
End Function
0
Comment
Question by:posae
1 Comment
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39705559
Have you tried checking the attachment type property which has the properties:

OlAttachmentType:
     olByReference
     olByValue
     olEmbeddeditemolOLE

This method might be enough for you but if doesn't resolve correctly, (its not perfect) then assuming you are using 2007 or later you can try the PropertyAccessor in outlook for example:

olAtt.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E")


Chris
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Outlook 2013 has lost the address book or contacts list 8 16
OCT or Config.xml 2 33
Office 2016 User Guides 5 33
OUTLOOK, KERBERO, NTLM 1 27
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now