vba 将特定文件类型保存为带有接收日期时间的附件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26508389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:56:41  来源:igfitidea点击:

Save specific file type as attachment with received date time

vbaemail-attachmentsoutlook-vba

提问by Stu Shapiro

  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"
  1. 仅将图像保存到文件夹中,即 .jpg .jpeg .gif .png
  2. 包括接收日期
  3. 将所有保存的图像文件类型重命名为“.jpg”

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

我已经放下了大部分。它正在保存这样的文件: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

回答by JNevill

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 ifstatement 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.

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