问题
- save only images to a folder i.e .jpg .jpeg .gif .png
- Include the received date
- 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