Save Mail attachment to folder with month in filename?

NessPJ

Active Member
Joined
May 10, 2011
Messages
431
Office Version
  1. 365
Hi,

I tried to make/search a code that could help me quickly save an attachment to a fixed folder that also adds the 2 digits of the month to the filename.

When i run the routine shown below nothing is happening. Am i doing something wrong?

VBA Code:
Sub SaveAttachmentWithDate()

Dim date_now As Date
Dim dateStamp As String
Dim LRandomNumber As Integer

For i = lngCount To 1 Step -1

    'Set the Folder to save the Attachment:
    strFolderpath = "C:\Temp\Attachments\"
    
    ' Get the file name.
    strFile = objAttachments.Item(i).FileName
    
    'LRandomNumber = Int((300 - 200 + 1) * Rnd + 200)       Not used for now
    
    date_obj = objMsg.ReceivedTime  ' Now()
    dateStamp = Format(date_obj, "mm")

     ' Combine with the path to the folder.
    strFile = strFolderpath & strFile & dateStamp

    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile

Next i

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Oops... my bad thats a professional google searching mistake.

Do you know which function is used to count the number of attachments in a mail? :)
 
Upvote 0
I made an adaptation to this code but its still not working. I am using the Attachments property now.
Am i still doing something wrong?

VBA Code:
Sub SaveAttachmentWithDate()

Dim myattachments As Outlook.Attachments
Dim MyAttachment As Attachment

Dim date_now As Date
Dim dateStamp As String
Dim LRandomNumber As Integer
 
        Set myattachments = OutMail.Attachments
 
        For Each MyAttachment In myattachments
        
        If MyAttachment > 0 Then     

                'Set the Folder to save the Attachment:
                strFolderpath = "C:\Temp\Attachments\"
    
                'Get the file name.
                strFile = objAttachments.Item(i).FileName
    
                'LRandomNumber = Int((300 - 200 + 1) * Rnd + 200)       Not used for now
    
                date_obj = objMsg.ReceivedTime  ' Now()
                dateStamp = Format(date_obj, "yyyymm")

                'Combine with the path to the folder.
                strFile = strFolderpath & strFile & dateStamp

                ' Save the attachment as a file.
                objAttachments.Item(i).SaveAsFile strFile
               
        Else:
            Exit Sub
            
        End If
    
        Next MyAttachment

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,143
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top