Reputation: 21
I'm setting up a script in outlook that saves some PDF attachments as the date they were received. This will save the file to the desired location but it wont name it as the date received how would i add this in?
There is probably a lot of unused code in here as i got it from another website and have removed a few things i don't want such as deleting the attachment after its saved.
Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
' Get the path to your My Documents folder
strFolderpath = "D:\Documents\"
On Error Resume Next
' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection
' Set the Attachment folder.
strFolderpath = strFolderpath
' Check each selected item for attachments. If attachments exist,
' save them to the strFolderPath folder and strip them from the item.
For Each objMsg In objSelection
' This code only strips attachments from mail items.
' If objMsg.class=olMail Then
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count
strDeletedFiles = ""
If lngCount > 0 Then
' We need to use a count down loop for removing items
' from a collection. Otherwise, the loop counter gets
' confused and only every other item is removed.
For i = lngCount To 1 Step -1
' Save attachment before deleting from item.
' Get the file name.
strFile = objAttachments.Item(i).FileName
' Combine with the path to the Temp folder.
strFile = strFolderpath & strFile
' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile
'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If
'Use the MsgBox command to troubleshoot. Remove it from the final code.
'MsgBox strDeletedFiles
Next i
' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = vbCrLf & "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
Else
objMsg.HTMLBody = "<p>" & "The file(s) were saved to " & strDeletedFiles & "</p>" & objMsg.HTMLBody
End If
objMsg.Save
End If
Next
ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
Upvotes: 0
Views: 725
Reputation: 759
add the following function to your code:
Function Dateiendung(vDateiname As String) As String
Dim Wortlaenge As Integer
Dim StellePunkt As Integer
Wortlaenge = Len(vDateiname) ' Anzahl Zeichen des Dateinamens
StellePunkt = InStrRev(vDateiname, ".") ' Anzahl Zeichen vor dem letzten Punkt
Dateiendung = Right(vDateiname, Wortlaenge - StellePunkt) ' Dateiendung wird extrahiert
End Function
add this line at the beginning of your code:
dim fileext as string
Instead of:
strFile = objAttachments.Item(i).FileName
you have to put:
fileext = Dateiendung(objMailSel.Attachments.item(i).FileName)
strfile = Mid(objMailSel.Attachments.item(i).FileName, 1, Len(objMailSel.Attachments.item(i).FileName) - Len(fileext) - 1) & " " & Format(Date, "MMddYYYY") & " ." & fileext
Upvotes: 0
Reputation: 2451
Anything you want to change with the filename would be in this modified snippet,
strFile = strFolderpath & objAttachments.Item(i).FileName 'Add the folder and filename
strFile = left(strfile, len(strFile)-4) 'Strip the .PDF
strFile = strFile & format(Date, "MMddYYYY") & ".PDF" 'Add the date and readd .PDF
objAttachments.Item(i).SaveAsFile strFile
For different date formatting to try out, check out this
Upvotes: 1