Avatar of dwe0608
dwe0608
Flag for Australia asked on

Saving Outlook Attachments - but not the inline images

Hi Guys,

Looking for some assistance in avoiding saving inline images (images which show in an email as opposed to those which are attached).

I am using the following code for saving the attachments to an email and it works fine - but includes ALL attachments, including inline images. How can I avoid that?

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem, strFolderPath As String)
    Dim oAttachment As Outlook.attachment
    Dim sSaveFolder As String
    Dim fso As New Scripting.FileSystemObject
    Dim bName As String
    Dim sSaveFolder As String
    
    'Checking that path ends in \
    If Right(strFolderPath, 1) = "\" Then
        ' fall through
    Else:
        strFolderPath = strFolderPath & "\"
    End If
    
    sSaveFolder = strFolderPath
    
    For Each oAttachment In MItem.Attachments
        ' change the file name with the index
        'Debug.Print "DisplayName: " & oAttachment.DisplayName
        'Debug.Print "FileName: " & oAttachment.FileName
        'Debug.Print "Pathname: " & oAttachment.PathName
        ' get the filename (no extension)
        bName = fso.GetBaseName(oAttachment.DisplayName)
        ' rename the file with no extension using the index as the order
        ' to be merged at a future date
        bName = oAttachment.Index & "-" & bName
        ' put the extension back on
        bName = bName & "." & fso.GetExtensionName(oAttachment.DisplayName)
        ' save the file with the new name
        oAttachment.SaveAsFile sSaveFolder & bName
    Next
    
End Sub

Open in new window


Many thanks

DWE
Microsoft OfficeOutlookVBA

Avatar of undefined
Last Comment
jalimocho o

8/22/2022 - Mon
Bill Prew

Try filtering them out by attachment type.

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem, strFolderPath As String)
    Dim oAttachment As Outlook.attachment
    Dim sSaveFolder As String
    Dim fso As New Scripting.FileSystemObject
    Dim bName As String
    Dim sSaveFolder As String
    
    'Checking that path ends in \
    If Right(strFolderPath, 1) = "\" Then
        ' fall through
    Else:
        strFolderPath = strFolderPath & "\"
    End If
    
    sSaveFolder = strFolderPath
    
    For Each oAttachment In MItem.Attachments
        If oAttachment.Type <> olEmbeddeditem Then
            ' change the file name with the index
            'Debug.Print "DisplayName: " & oAttachment.DisplayName
            'Debug.Print "FileName: " & oAttachment.FileName
            'Debug.Print "Pathname: " & oAttachment.PathName
            ' get the filename (no extension)
            bName = fso.GetBaseName(oAttachment.DisplayName)
            ' rename the file with no extension using the index as the order
            ' to be merged at a future date
            bName = oAttachment.Index & "-" & bName
            ' put the extension back on
            bName = bName & "." & fso.GetExtensionName(oAttachment.DisplayName)
            ' save the file with the new name
            oAttachment.SaveAsFile sSaveFolder & bName
        End If
    Next
    
End Sub

Open in new window


»bp
SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Alexei Kuznetsov

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dwe0608

ASKER
Hi Bill ... thanks for the help so far ... can you explain to me the difference between the two coded solutions you have provided ... ie oAttachment.Fields(&H3712001E) = "" and oAttachment.Type <> olEmbeddeditem ...

MTIA

DWE
dwe0608

ASKER
Hi Alexei ... thanks for the input ... how would you incorporate your suggestion into the code ... currently I am using a small function which is working to an extent ...

Public Function IsHiddenAttachment(olkAtt As Outlook.attachment) As Boolean
    ' Purpose: Determines if an attachment is a hidden attachment.
    'Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E" - original
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B" - altered
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (Trim(varTemp) <> "")
    'Debug.Print varTemp
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Open in new window


MTIA

DWE
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Alexei Kuznetsov

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dwe0608

ASKER
Thanks guys, a combination of suggestions worked ...:-)
jalimocho o

Dear dwe0608,
In case you were able to make it work, would you mind sharing the code?
I'm also facing a similar problem. I want to count the number of attachments in an email excluding the inline ones, but every hint I find in internet seems to work fine in older versions of Outlook but not in Outlook 2016.

Thank you,

Jalimocho