I have a macro that saves attachments from email to my c:\
The problem I'm havign is that I get a number of files with identical names during a day. This macro simply overwrites the previous attachment with the newest. I'd like to modify so a time stamp is added to the file name, hopefully the time the email was recieved.
Any help is appreciated
Sub SaveAttach() 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 = CreateObject("WScript.Shell").SpecialFolders(16) 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. blnOverwrite = False ' False = don't overwrite, True = do overwrite strFolderpath = "C:\backup\" ' path to target folder 'MsgBox strFolderpath ' Check each selected item for attachments. ' If attachments exist, save them to the Temp ' 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 'MsgBox objAttachments.Count 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 ' Delete the attachment. objAttachments.Item(i).Delete '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 & "" Else strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _ strFile & "'>" & strFile & "</a>" End If 'MsgBox strDeletedFiles Next i ' End If ' Adds the filename string to the message body and save it ' Check for HTML body If objMsg.BodyFormat <> olFormatHTML Then objMsg.Body = objMsg.Body & vbCrLf & _ "The file(s) were saved to " & strDeletedFiles Else objMsg.HTMLBody = objMsg.HTMLBody & "" objMsg.HTMLBody = objMsg.HTMLBody & "<p>" & _ "The file(s) were saved to " & strDeletedFiles & "</p>" End If objMsg.Save End If Next ExitSub: Set objAttachments = Nothing Set objMsg = Nothing Set objSelection = Nothing Set objOL = Nothing End Sub
Last edited by davesexcel; 04-20-2010 at 09:04 AM.
I think this should return the time the email was received:
Format(objMsg.ReceivedTime, "hh:mm:ss")
You can then add that to the file name.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks