Save outlook attachments and rename/append files with identifier from subject line

Status
Not open for further replies.

CalGal80

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server 2010
Im really new to VBA and need some help. I'm trying to write a VBA script (along with a Outlook rule) to automatically download attachments from daily emails that contain multiple attachements and append the file names with the date that always appears at the end of the subject line.

This is what the subject line looks like - "Email Alert for Department for 10/20/2014". I just need to isolate the rightmost 10 spaces that indicates the run date of the files.

So I found code online that works to automatically download the attachments and append by current date which does work. See below.

Code:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyymmdd ")
saveFolder = "Z:\Daily Emails"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub

I tried to modify the code to append the file name by using the last 10 spaces of the subject line instead of the current date but when I ran the rule with the script it did not work.

Code:
Public Sub saveAttachtoDiskCL(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim savefolder As String
Dim Subject As String
savefolder = "Z:\Daily Emails"
     For Each objAtt In itm.Attachments
Subject = itm.Subject

'Isolate last 10 spaces
Subject = Right(itm.Subject, 10)
objAtt.SaveAsFile savefolder & "\" & objAtt.DisplayName & Subject
Set objAtt = Nothing
Next
End Sub

I feel like its something small I'm overlooking. Any thoughts?
 

CalGal80

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server 2010
Thanks for the tip but I just fixed it and it still doesnt work. What I did notice was that the progress bar moved faster but still nothing being saved into my folder.

Im running the rule with the script on emails that have already been received so Im looking to auto download all attachments from historical emails. Do you think that is making a difference?

Anyone else with ideas?

here is the modified code.

Code:
Public Sub saveAttachtoDiskCL(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim savefolder As String
Dim Subject As String
savefolder = "Z:\Daily Emails"
     For Each objAtt In itm.Attachments
Subject = itm.Subject

'Isolate last 10 spaces
Subject = Right(itm.Subject, 10)
objAtt.SaveAsFile savefolder & "\" & Subject & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Set a breakpoint into the code so the execution stops there, then go through it step by step with f8. Especially see if savefolder & "\" & Subject & objAtt.DisplayName is a valid file name. Also, the folder in savefolder must exist already.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
W Save and rename outlook email attachments to include domain name & date received Outlook VBA and Custom Forms 4
C Auto save outlook attachments when email is received Outlook VBA and Custom Forms 1
Diane Poremsky Edit and Save Outlook's Read-Only Attachments New Slipstick.com Articles 0
S using script rule to save attachments on arrival Outlook 2010 Outlook VBA and Custom Forms 9
R Outlook 2013 VB rule to auto save attachments with different file types Outlook VBA and Custom Forms 5
E Outlook prompts to save attachments when no changes have been made. Using Outlook 0
A File - Save Attachments does nothing in Outlook 2003 with Exchange 2010 ... Using Outlook 3
O closing outlook prompts to save all opened attachments Using Outlook 2
M Outlook 2007 - Save outgoing attachments to network drive on sending? Using Outlook 1
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
P Outlook pst file is too huge with POP3. How to save more space? Using Outlook 4
W Outlook Calendar does not save view any longer! Using Outlook 3
9 Outlook 2016 How to save an Outlook attachment to a specific folder then delete the email it came from? Using Outlook 1
W Save Outlook attachment in network folder and rename to current date and time Outlook VBA and Custom Forms 18
C Outlook - cannot save subject line changes Using Outlook 2
I Outlook 2016 64bit - on receipt convert emails into PDF and save Outlook VBA and Custom Forms 2
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
Diane Poremsky Export (Save) Outlook Contact photos New Slipstick.com Articles 0
E Outlook 2016 and Numerous Prompts to Save Emails Using Outlook 3
Diane Poremsky Save Outlook Email as a PDF New Slipstick.com Articles 0
L Save message from outlook to desktop in 2013 outlook Outlook VBA and Custom Forms 1
Mark Foley Where are Outlook categories save for IMAP? Using Outlook 12
J Auto-Save for Outlook Calendar Entries Using Outlook 5
L Outlook 2007 Macro Save Contact Using Outlook 10
B Outlook 2007 Save Current View,Font,Toolbar Settings Using Outlook 6
F Outlook 2010 custom font style duplicates itself every time I save a task Using Outlook 0
C How To Open Outlook Attachment in Excel, Modify some Data, then Re-Save It Using Outlook 3
B Outlook 2010 won't save 'current view' with reading pane at 'bottom' Using Outlook 4
O Export (save) Outlook Contact photos Using Outlook 2
E can't save draft email in Outlook 2010 Using Outlook 2
F CAN'T SAVE OUTLOOK 2010 E-MAIL TEMPLATE (.oft) Using Outlook 1
S Save Attachement from outlook Using Outlook 2
Rupert Dragwater Can't save font type in Outlook 2010 Using Outlook 3
S Outlook 2010: Cannot save forms templates to folder list Using Outlook 4
D outlook to save copy of hotmail? Using Outlook 3
N Save new contact from inbox to contact subfolder in Outlook 2010 Using Outlook 1
M Outlook doesn't save sent emails Using Outlook 1
R Error message "Cannot save free/busy information" when changing or deleting appointments in Outlook. Using Outlook 4
S Outlook 2010: Save POP email to Exchange mailbox?? Using Outlook 3
T Outlook 2010 does not save "sent" messages Using Outlook 1
K Outlook: Can not save free / busy information error message Using Outlook 2
C Outlook 2010 & windows 7 - WILL NOT SAVE emails on the server. Using Outlook 1
A Outlook 2010 trying to create calendar/task entries generates error "Could not save item.The connect Using Outlook 2
S Outlook 14 lose contact with our exchange server 2010 after Windows 7 have been in power save mode. Using Outlook 2
R Save Outlook Field History Using Outlook 2
7 I would like to save my email messages from microsoft outlook to a cdrw. Using Outlook 3
A Outlook 2010 won't save passwords Using Outlook 3
P Outlook deletes my email account passwords even when I check 'save password' box Using Outlook 2
K Outlook doesn't save outgoing emails. Using Outlook 2
M Outlook 2010 asks me "Do you want to open or save this file?" when I first run it. If I open, it con Using Outlook 3

Similar threads

Top