[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

Extract Attachments from .msg messages but exclude embedded

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
posae
Asked:
posae
1 Solution
 
Chris BottomleyCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now