Solved

Outlook VBA - save attachment using email Subject as filename

Posted on 2010-11-15
9
1,026 Views
Last Modified: 2012-06-27
I have a piece of VBA script that saves attachments, and is activated using outlook Rules.

The attachment I'm interested in comes once a day and always has the same name, so I want to concatenate the subject of the email - which does change each day (or concatenate the date of the email).

I have never done VBA before and the following *works* as long as 'emailsubject' is set equal to a "string like this":


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

emailsubject = "string like this"

saveFolder = "D:\Users\AAA\Reporting\"
    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & emailsubject & objAtt.DisplayName
        Set objAtt = Nothing
    Next
End Sub



I have tried setting emailsubject equal to each of the following :
emailsubject = Outlook.olMailItem.Subject
emailsubject = Item.Subject
emailsubject = olMailItem.Subject
emailsubject = MailItem.Subject
emailsubject = objMsg.Subject

But none of these work - what is wrong / missing?
Many thanks
Quentin
0
Comment
Question by:quentinp
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:spinzr0
ID: 34138693
It should be itm.Subject
0
 

Author Comment

by:quentinp
ID: 34139215
Thanks - but that's not working either, though it does pause in the same way as when it *is* working.  Just no file in the folder.  
I've realized that the Subject probably has some characters in it that will mess things up :

"/" and ":"

Alternatively - can I insert the date of the email?


0
 
LVL 8

Expert Comment

by:spinzr0
ID: 34139602
In that case, it would be itm.ReceivedTime.  You'd still need to replace the \ and :.  So I would use
Replace(Replace(item.ReceivedTime, "/", "-"), ":", ".")
0
 

Author Comment

by:quentinp
ID: 34140055
Thanks - still not working though.  I have :

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim Msg As Outlook.MailItem
Dim saveFolder As String
Dim emailsubject As String

emailsubject = Replace(Replace(itm.ReceivedTime, "/", "-"), ":", ".")

saveFolder = "D:\Users\AAA\Reporting\"
   
    For Each objAtt In itm.Attachments
   
        objAtt.SaveAsFile saveFolder & "\" & emailsubject & objAtt.DisplayName
       
        Set objAtt = Nothing

    Next

End Sub



I tried
emailsubject = Replace(Replace(item.ReceivedTime, "/", "-"), ":", ".")
emailsubject = Replace(Replace(itm.ReceivedTime, "/", "-"), ":", ".")
emailsubject = Replace(Replace(itm.CreationTime, "/", "-"), ":", ".")
emailsubject = itm.class
emailsubject = Msg.class
emailsubject = itm.Importance
emailsubject = Msg.Importance


in the above - (I was hoping to hit a property that didn't need the Replace function) and none of them worked.

For debugging is there a way to find out if I am getting a value for "emailsubject" - before I try to use it in a filename?  (Can I show it onscreen?)

Many thanks
Quentin
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 8

Expert Comment

by:spinzr0
ID: 34140088
emailsubject = Replace(Replace(itm.ReceivedTime, "/", "-"), ":", ".")

msgbox emailsubject
0
 

Author Comment

by:quentinp
ID: 34140395
Something weird is going on - after this didn't work I've gone all the way back to the original script that did work, and still no luck.
So something else has changed in the meantime.  
I've restarted Outlook, still no luck.  will have another go at this tomorrow.
Many thanks
Quentin
0
 
LVL 8

Expert Comment

by:spinzr0
ID: 34161763
Any luck?  Happy to still help.
0
 

Accepted Solution

by:
quentinp earned 0 total points
ID: 36892327
Never got this to work, but the need went away
0
 

Author Closing Comment

by:quentinp
ID: 36915489
Never resolved, but the need went away.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now