Save specific file type as attachment with received date time

你。 提交于 2019-12-11 22:24:23

问题


  1. save only images to a folder i.e .jpg .jpeg .gif .png
  2. Include the received date
  3. rename all saved image filetypes to ".jpg"

I have most of it down. It is saving files like this: test.jpeg.jpg and test.jpg.jpg

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat As String
    Dim strFileExtension As String

    saveFolder = "C:\emails\"
    dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
    strFileExtension = ".jpg"

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName & strFileExtension
        Set objAtt = Nothing
    Next
End Sub

回答1:


Something like the following would work:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat As String
    Dim strFileExtension As String
    Dim strSaveFileName as string

    saveFolder = "C:\emails\"
    dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd Hmm ")
    strFileExtension = ".jpg"

    For Each objAtt In itm.Attachments
        if lcase(right(objAtt.FileName, 4)) = "jpeg" or lcase(right(obtAtt.FileName, 3) = "jpg") then

            strSaveFileName = mid(objAtt.FileName, instr(1, objAtt.FileName, ".", length(objAtt.FileName) - instr(1, obtAtt.FileName)) & strFileExtension
            objAtt.SaveAsFile saveFolder & "\" & dateFormat & strSaveFileName
            Set objAtt = Nothing
        End if
    Next
End Sub

This has an added if statement to test for the file extension being JPG or JPEG. If it is, then it uses some string functions to grab the bits of the filename before the extension and uses that in the final saveasfile.



来源:https://stackoverflow.com/questions/26508389/save-specific-file-type-as-attachment-with-received-date-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!