Avatar of quentinp
quentinp
Flag for United States of America asked on

Outlook VBA - save attachment using email Subject as filename

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
VB ScriptOutlookEmail Clients

Avatar of undefined
Last Comment
quentinp

8/22/2022 - Mon
spinzr0

It should be itm.Subject
quentinp

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


spinzr0

In that case, it would be itm.ReceivedTime.  You'd still need to replace the \ and :.  So I would use
Replace(Replace(item.ReceivedTime, "/", "-"), ":", ".")
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
quentinp

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

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

msgbox emailsubject
quentinp

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
spinzr0

Any luck?  Happy to still help.
ASKER CERTIFIED SOLUTION
quentinp

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
quentinp

ASKER
Never resolved, but the need went away.