Outlook 2013 VB rule to auto save attachments with different file types

Status
Not open for further replies.

Ruben David

Member
Outlook version
Outlook 2013 32 bit
Email Account
Exchange Server
I have a script in place:

Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Public Sub PauseApp(PauseInSeconds As Long)
Call AppSleep(PauseInSeconds * 1000)
End Sub

Public Sub saveAttachtoDisk(Item As Outlook.MailItem)

Dim saveFolder As String
saveFolder = "c:\InvItems"

Dim dateFormat
dateFormat = Format(Now, "mmddyyyy-Hmmss")

Dim Subject As Variant
Subject = Item.Subject

Dim objAtt As Outlook.Attachment
For Each objAtt In Item.Attachments
objAtt.SaveAsFile saveFolder & "\" & Subject & "-" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing

Next
PauseApp 3
End Sub


The emails that come to me all have the pretty much the same subject name (Inv Report date of report etc...) the only difference is some come as .xml, xls, and doc.

The .xml files have a very long filename and it can't be changed when it sends to me so I tried to adapt to this by testing this code:

Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Public Sub PauseApp(PauseInSeconds As Long)
Call AppSleep(PauseInSeconds * 1000)
End Sub
Public Sub saveAttachtoDisk(Item As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
Dim Subject As Variant
Dim strFileExtension As String
saveFolder = "c:\InvItems"
dateFormat = Format(Now, "mmddyyyy-Hmmss")
Subject = Item.Subject

For Each objAtt In Item.Attachments
If InStr(objAtt.DisplayName, ".xml") Then
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName

End If

Set objAtt = Nothing

Next
PauseApp 1
End Sub


This works but when I try to add additional code for the other file types to save with subject & date attached it doesn't work. I only want the .xml files to save as the filename but the other file types I need to keep the subject. I can use the first script and I get all the files save to my drive but the .xml files are not saving. I'm thinking it's because the filename is already long and I can't change this because another script picks up the files based on the subject name. The reason it worked before is because the .xml files weren't in the loop but now I get 15 of them a day that I need to save as well.

This is a rule not a macro because the plan is to have it running on a separate pc when I get this finalized.

Help me Obi Wan you're my only hope.
 
Wow! that worked - duh! but if I wanted to add another file type to that would I write it like this:

Dim objAtt As Outlook.Attachment
For Each objAtt In Item.Attachments
If InStr(objAtt.DisplayName, ".xml", "*.doc") Then
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Else
objAtt.SaveAsFile saveFolder & "\" & Subject & "-" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing

because the .doc files look like this rpfcu.thedate.rp.doc and I need the entire filename

Thank you Michael
 
You cannot pass two search strings to the Instr function. If all file types have three-digit extension, I'd use this one:
Code:
select case right$(objAtt.DisplayName, 4)
case ".xml"
case ".doc"
case else
end select
 
Sorry I'm a little more confused and the .doc is actually xxxx.thedate.rp.doc and I need to keep that rp in the save file. I'm just not sure how to use your reference above.
 
Here
Code:
If InStr(objAtt.DisplayName, ".xml", "*.doc") Then
you're trying to search for '.xml' and for '*.doc' in the DisplayName, which doesn't work.

The shown Select Case is a way to do what you want, that is do something if the DisplayName ends with '.xml' or with '.doc', and do something else if it ends with anything else. It doesn't change the DisplayName, it just analyzes it.

If you want to do the same for both '.xml' and '.doc', you can also write it this way:
Code:
select case right$(objAtt.DisplayName, 4)
case ".xml", ".doc"
    ' do something
case else
    ' do something else
end select
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
I Create custom Outlook 2013 Rule in Office 365 Outlook VBA and Custom Forms 5
Mary B Outlook 2013: Rule for copying new email to folder & marking that copy as read Using Outlook 1
Gary Brown Outlook 2013 VBA to run a rule Outlook VBA and Custom Forms 13
P now on office 365 but getting error messages about missing Outlook 2013 cache folders Using Outlook 2
P Outlook 2013 search no longer works Using Outlook 2
K Closing external IMAP email... Outlook 2013 Using Outlook 0
P Outlook 2013 Calendar Archiving Using Outlook 0
T Compuserve, Yahoo, Oath2 and Outlook 2013 Using Outlook 4
Paula S. Outlook 2013 won't recognize new password Using Outlook 4
B Outlook 2013 erratically deleting original file that is attached Using Outlook 0
X Using Outlook 2013 and Outlook 365 Using Outlook 1
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
J Outlook 2013 Change color of text in data fields of contacts in Outlook 2013? Using Outlook 10
Commodore Outlook (2013) issues with icloud.com mail Using Outlook 0
D iCloud Add-in not working in Outlook 2013 and Outlook 2016 After Windows Upgrade & iCloud Upgrade Using Outlook 2
P Outlook 2013 "Item could not be moved - still an issue for Outlook 2013 Using Outlook 0
M Creating an RSS Feed **FROM** Outlook 2013 Calendar. Using Outlook 5
D Outlook 2013 Yahoo IMAP Sync problems Using Outlook 1
iwshim outlook 2013 - I cannot see the "Manage Add-ins" Using Outlook 2
B Outlook 2013/Exchange 2013 - Conf Rooms not fully booking "resolved conflict" meetings Using Outlook 3
G Can't create Folder Groups in Outlook 2013 Using Outlook 0
A Run time error 424. object required in outlook 2013 Outlook VBA and Custom Forms 10
Rupert Dragwater Several questions regarding Outlook 2013 Using Outlook 9
P Outlook 2013 opens with FILE tab selected. Using Outlook 3
I How to display sender's name instead of email address in outlook 2013 message Using Outlook 5
Z Outlook 2013 Gmail IMPA emails not showing dates only showing times Using Outlook 1
B Forward every other email in Outlook 2013 Outlook VBA and Custom Forms 2
P Synching Outlook 2013 Notes with iPhone 8 Using Outlook 1
N Outlook 2013 Folder View Using Outlook 8
M Outlook 2013 fails to start -- missing WindowsCodecs.dll error Using Outlook 3
David Michaels Contact card shows blank Outlook 2013 Outlook VBA and Custom Forms 0
J Calendar data can be viewed on Outlook.com, but no longer syncs to Outlook 2013 Using Outlook.com accounts in Outlook 3
C Attached files on Outlook 2010 Sp1 & Exc 2013 are missing Exchange Server Administration 1
R Folders shifting around in Outlook 2013 and 2016 Using Outlook 3
P Sync Desktop Outlook 2013 with Outlook.com? Using Outlook.com accounts in Outlook 5
J Outlook 2013 when clicking To in a new email show Company names Using Outlook 0
J Outlook - 2013 - Error msg when copying folders from Online Archives to another user's mailbox Using Outlook 0
D Outlook 2013 changing iCloud reminder time? Using Outlook 0
M OUTLOOK 2013 CONTACT PROPERTIES Using Outlook 0
J Object Variable or With Block Not Set Error in Outlook 2016 and not Outlook 2013 Outlook VBA and Custom Forms 3
P Outlook 2013 puts replies in Outbox Using Outlook 1
P Contacts gone missing in Outlook 2013 POP Using Outlook 4
M Outlook 2013 Using Outlook 1
P MS OUTLOOK 2013 - Adding Sender on the CC line Using Outlook 5
P MS OFFICE 2013 - Outlook locking up when using the sent on behalf of feature in VB Using Outlook 3
I Outlook 2010, 2013 will not open .msg or .eml files Using Outlook.com accounts in Outlook 1
Jeff Davis Help fix my Outlook 2013? Using Outlook 28
J Outlook 2013 hanging for unknown reason Using Outlook 1
O Outlook 2013 Problems sending large mails Using Outlook 4
K Rebuilding Outlook 2013 from Outlook.com after OS rebuild Using Outlook.com accounts in Outlook 2

Similar threads

Back
Top