Solved

Extract Attachments from .msg messages but exclude embedded

Posted on 2013-12-08
1
545 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

12 Experts available now in Live!

Get 1:1 Help Now