Solved

Outlook VBA - save attachment using email Subject as filename

Posted on 2010-11-15
9
1,091 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you irritated by repeating emails issue in Microsoft Outlook 2016 after recent update ?  Lets’ see how to resolve and prevent duplicate emails in the Outlook 2016 using some simple techniques.
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
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: …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

820 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