Solved

Outlook VBA - save attachment using email Subject as filename

Posted on 2010-11-15
9
1,171 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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.

Question has a verified solution.

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

In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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.

688 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