PDA

View Full Version : Solved: Outlook VBA - Renaming attachment as mail subject



Nyanko
05-29-2013, 05:18 AM
I've been working with Excel VBA for years and now have a need to do a simple piece of work in Outlook.

I'm using the following code to save an attachment to a specific folder. However I need to rename the attachment as the subject of the email.


Private Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String

saveFolder = "C:\Temp\"

For Each objAtt In itm.Attachments
If objAtt.FileName <> "image001.gif" Then
objAtt.SaveAsFile saveFolder & "\" & objAtt.FileName
End If

Set objAtt = Nothing
Next
End Sub


I've tried researching but can't find the answer to something so simple ! And every effort I try results in a run time error - help :)

Nyanko
05-29-2013, 05:30 AM
Ok... So found out the answer in this forum :


Private Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Temp\"
MSN = Trim(itm.Subject)

For Each objAtt In itm.Attachments
If objAtt.FileName <> "image001.gif" Then
objAtt.SaveAsFile saveFolder & "\" & itm.Subject & ".JPG"
End If

Set objAtt = Nothing
Next
End Sub