?
Solved

Outlook VBA - save attachment using email Subject as filename

Posted on 2010-11-15
9
Medium Priority
?
1,236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Changing a few Outlook Options can help keep you organized!
We aren’t perfect, just like everyone else.  Check out the email errors our community caught and learn the top errors every email marketer should avoid.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 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