简体   繁体   中英

Outlook attachments - Save with Specific Name / Specific type - VBA code

Main problem it only renames the first attachment and I have no control over the other items in the email.

This code saves my attachment and renames it. It works IF the email has only one attachment and no images in signature. If the email comes with one Excel file and an image in signature, it renames the image what I intended to be the Excel file name, and then leaves the Excel file with its original name.

Would be awesome if I can also dictate specific extensions in the save.

Public Sub saveAttachtoDisk_Vendor(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim fso As Object
Dim oldName

Dim file As String
Dim DateFormat As String
Dim newName As String

Dim enviro As String
saveFolder = "S:\Test\"

Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next

For Each objAtt In itm.Attachments
    file = saveFolder & objAtt.DisplayName
    objAtt.SaveAsFile file

    Set oldName = fso.GetFile(file)
    DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ")
    newName = "Vendor.xls"
    oldName.Name = newName

    Set objAtt = Nothing
Next

Set fso = Nothing
End Sub

I am toying with these codes but cant get them to work.

validExtString = ".doc .docx .xls .xlsx .msg .pdf .txt" ' <---- Update as needed
validExtArray = Split(validExtString, " ")

And this.

If Right(Atmt.FileName, 3) = "xls" Then
     FileName = "C:\Email Attachments\" & Atmt.FileName
     Atmt.SaveAsFile FileName
     i = i + 1
End If

Be aware, the folder can't contain files with the same name. You need to use different names for attachments.

For Each objAtt In itm.Attachments

The code iterates over all attachments and save them on the disk. It looks like an error in code doesn't allow to get the job done.

I'd recommend running the code in the step-by-step manner (F8) and see what happens in the code running it under the debugger. See Getting Started with VBA in Outlook 2010 for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM