?
Solved

Extract Attachments from .msg messages but exclude embedded

Posted on 2013-12-08
1
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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