将特定文件类型保存为附件,并附上日期时间

时间:2014-10-22 13:19:06

标签: vba email-attachments outlook-vba

  1. 仅将图像保存到文件夹,即.jpg .jpeg .gif .png
  2. 包括收到的日期
  3. 将所有已保存的图像文件类型重命名为“.jpg”
  4. 我大部分都失望了。它正在保存这样的文件:test.jpeg.jpg和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 个答案:

答案 0 :(得分:0)

以下内容可行:

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

这有一个添加的if语句来测试文件扩展名为JPG或JPEG。如果是,那么它使用一些字符串函数来获取扩展名之前的文件名位,并在最终saveasfile中使用它。