Tom Skowyrski
asked on
We need a macro for saving attachments with name of that file and one more thing from the body
We receive emails with pdf invoices and need to save them to the folder. As mentioned we need to save it with current filename and add from the message body the invoice ID. It looks like this in the message: "The invoice number is: 7489. The invoice ID is: 5088." So we need that file to be named like this: 'current filename - invoice ID.pdf'. We use a macro to save those files and we'd like to ask how to modify it to get what we want. This is the macro we've been currently using:
Sub SaveAttachmentsFromSelecte dMailItems ()
Dim individualItem As Object
Dim att As Attachment
Dim strPath As String
Dim strFileName As String
Dim strExt As String
Dim dicFileNames As Object
strPath = "C:\Test\"
Set dicFileNames = CreateObject("Scripting.Di ctionary")
For Each individualItem In Application.ActiveExplorer .Selection
If TypeName(individualItem) = "MailItem" Then
For Each att In individualItem.Attachments
If Not dicFileNames.exists(att.Fi leName) Then
dicFileNames.Add att.FileName, 1
Else
If LCase(Right(strFile, 4)) <> ".pdf"
dicFileNames(att.FileName) = dicFileNames(att.FileName) + 1
End If
strFileName = Split(att.FileName, ".")(0)
strExt = Split(att.FileName, ".")(1)
att.SaveAsFile strPath & strFileName & "-" & dicFileNames(att.FileName) & "." & strExt
Next att
End If
Next individualItem
Sub SaveAttachmentsFromSelecte
Dim individualItem As Object
Dim att As Attachment
Dim strPath As String
Dim strFileName As String
Dim strExt As String
Dim dicFileNames As Object
strPath = "C:\Test\"
Set dicFileNames = CreateObject("Scripting.Di
For Each individualItem In Application.ActiveExplorer
If TypeName(individualItem) = "MailItem" Then
For Each att In individualItem.Attachments
If Not dicFileNames.exists(att.Fi
dicFileNames.Add att.FileName, 1
Else
If LCase(Right(strFile, 4)) <> ".pdf"
dicFileNames(att.FileName)
End If
strFileName = Split(att.FileName, ".")(0)
strExt = Split(att.FileName, ".")(1)
att.SaveAsFile strPath & strFileName & "-" & dicFileNames(att.FileName)
Next att
End If
Next individualItem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No worries 👍. Thanks for the update
ASKER
Sorry for the delayed answer. The macro is working. Thank you very much for your help.
Kind regards,
Tom