Reputation: 1013
I would like to create a rule which run the following script:
Public Sub SaveToDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd")
saveFolder = "c:\temp\"
objAtt.SaveAsFile saveFolder & "\" & dateFormat & ".xls"
Set objAtt = Nothing
End Sub
This should save the attachment to the c:\temp folder but it does not work because objAtt value is nothing. For some reasons the line:
Dim objAtt As Outlook.Attachment
does not assign the attachment to the obAtt variable for a reason I can't figure it out. Here the attachment is a .csv file.
I am using outlook 2016 on windows 10. I would be glad to get any advice on why this is happening.
Upvotes: 1
Views: 338
Reputation: 14547
You have to set the object, because you where just creating it, not filling it.
So it had no clue it was supposed to be in any relation with the MailItem.
Further more, there is a collection of Attachement to go through, see :
Public Sub SaveToDisk(ItM As Outlook.MailItem)
Dim oAttS As Outlook.Attachments
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd")
saveFolder = "c:\temp\"
Set oAttS = ItM.Attachments
For Each objAtt In oAttS
objAtt.SaveAsFile saveFolder & "\" & dateFormat & "_" & objAtt.FileName
Next objAtt
Set oAttS = Nothing
Set objAtt = Nothing
End Sub
Upvotes: 4