none
outook attachements download RRS feed

  • Question

  • Hello, i'm using this VBA code to download the email attachements wich subject is "Candidature", the attachements are CVs

    and i want to rename the attachements with the sender email.

    can you please help me ?

    ****************************THE VBA CODE************************

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat
    dateFormat = Format(Now, "mm/dd H-mm")
    saveFolder = "C:\wamp64\www\Attachements"
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & dateFormat
    Set objAtt = Nothing
    Next
    End Sub

    Thursday, April 26, 2018 11:19 AM

Answers

  • Although there should not be illegal filename characters in the e-mail address, the following will ensure they are replaced with underscores

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim strFname As String
    Dim i As Integer
    Dim vBarred As Variant
    Const strBarred As String = "\,/,:,*,?,<,>,|,"""
    Const saveFolder As String = "C:\wamp64\www\Directory\"

        If itm.Attachments.Count > 0 Then
            For Each objAtt In itm.Attachments
                If objAtt.fileName Like "*.pdf" Then
                    strFname = itm.SenderEmailAddress & "-" & objAtt.fileName
                    vBarred = Split(strBarred, Chr(44))
                    For i = 0 To UBound(vBarred)
                        strFname = Replace(strFname, vBarred(i), Chr(95))
                    Next i
                    objAtt.SaveAsFile saveFolder & itm.SenderEmailAddress & "-" & objAtt.fileName
                    Exit For
                End If
            Next objAtt
        End If
    lbl_Exit:
        Set objAtt = Nothing
        Set itm = Nothing
        Exit Sub
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by FA1994 Thursday, May 3, 2018 9:25 PM
    Saturday, April 28, 2018 3:54 AM

All replies

  • There are a few issues here that need to be addressed first.

    Attachments also include any images in the message e.g. in the signature of the sender, so we need to be sure which attachment it is that you want to save. Do the attachments have a common name, or common format? If so what is it? Is there any possibility that there will be more than one attachment. If so what do you want to do about the second attachment?

    Your date format "mm/dd H-mm" has an illegal filename character so you cannot use that.

    You say you want to use the sender 'email'. By that do you want the email 'address'? If so do you want the domain name included? Do you want to keep the date in some form? If so how do you want to relate the date to the rest of the name.


    Graham Mayor - Word MVP
    www.gmayor.com

    Friday, April 27, 2018 1:50 PM
  • Hello, i edit the code like this:

    *******************************************

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "C:\wamp64\www\Directory"
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & itm.SenderEmailAddress + "-" + objAtt.FileName
    Set objAtt = Nothing
    Next
    End Sub

    ***********************************************

    the attachements will be CVs, so i will rename the cv with the email address attached to the fileName to conserve the extention.

    the attachements may not has a common name, and they also can be more than one attachement (CV+ motivation letter), so i want just to download the CV.

    concerning the format, the CVs will be "pdf" .

    any advices please ?




    • Edited by FA1994 Friday, April 27, 2018 2:20 PM
    Friday, April 27, 2018 2:14 PM
  • Although there should not be illegal filename characters in the e-mail address, the following will ensure they are replaced with underscores

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim strFname As String
    Dim i As Integer
    Dim vBarred As Variant
    Const strBarred As String = "\,/,:,*,?,<,>,|,"""
    Const saveFolder As String = "C:\wamp64\www\Directory\"

        If itm.Attachments.Count > 0 Then
            For Each objAtt In itm.Attachments
                If objAtt.fileName Like "*.pdf" Then
                    strFname = itm.SenderEmailAddress & "-" & objAtt.fileName
                    vBarred = Split(strBarred, Chr(44))
                    For i = 0 To UBound(vBarred)
                        strFname = Replace(strFname, vBarred(i), Chr(95))
                    Next i
                    objAtt.SaveAsFile saveFolder & itm.SenderEmailAddress & "-" & objAtt.fileName
                    Exit For
                End If
            Next objAtt
        End If
    lbl_Exit:
        Set objAtt = Nothing
        Set itm = Nothing
        Exit Sub
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by FA1994 Thursday, May 3, 2018 9:25 PM
    Saturday, April 28, 2018 3:54 AM
  • Thank you for your help
    I want also to ask if you have any idea how to extrract data from the pdf CVs


    • Edited by FA1994 Thursday, May 3, 2018 9:22 PM
    Thursday, May 3, 2018 9:22 PM
  • Thank you for your help
    I want also to ask if you have any idea how to extrract data from the pdf CVs



    Thursday, May 3, 2018 9:25 PM
  • It depends on the CVs.

    If they have random formatting, then  there is probably no automatic way to do that. If you wanted to process the CVs it would have been far better to distribute the CVS as Word forms using content controls and then extraction of the data would have been simple.

    With identical PDF formats, you may be able to open them in Word 2013 or later and treat them as Word documents - there are examples and tools for that on my web site.


    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, May 5, 2018 6:12 AM