Limiting length of saved attachment in VBA

Status
Not open for further replies.

rovercon

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
I have a simple macro that is saving email attachments. This has been working great, however an application that is accessing these files appears to have an issue with long file names (over 100 characters in length). I'm not a vba expert, but wondered if someone might know how to truncate/limit the file name length when macro runs and saves attachments. The first 30-40 characters include the subject, date, etc. so filenames are unique. I'm really trying to limit overall file name length for those few attachments with extremely long names. Any help appreciated! Thanks-

Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim i As Long
Dim dateFormat
saveFolder = "Z:\"
For Each objAtt In itm.Attachments
i = i + 1
dateFormat = Format(Now, "YYYY-mm-dd HH_mm_ss")
objAtt.SaveAsFile saveFolder & "\" & itm.Subject & "-" & dateFormat & "-" & i & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
 
Is there any field you want to shorten over the others?

you can use the left function:
filename = itm.Subject & "-" & dateFormat & "-" & i & objAtt.DisplayName
filename = left(filename, 98)

or
strSubject = left(itm.subject, 30)
objAtt.SaveAsFile saveFolder & "\" & strSubject & "-" & dateFormat & "-" & i & objAtt.DisplayName

The advantage in shortening the subject is that the file extension will not be removed - so you don't need to get it first and add it back.

For what it's worth, because you are using Now for the date stamp and including seconds, you shouldn't have a duplicate file name problem
 
Is there any field you want to shorten over the others?

you can use the left function:
filename = itm.Subject & "-" & dateFormat & "-" & i & objAtt.DisplayName
filename = left(filename, 98)

or
strSubject = left(itm.subject, 30)
objAtt.SaveAsFile saveFolder & "\" & strSubject & "-" & dateFormat & "-" & i & objAtt.DisplayName

The advantage in shortening the subject is that the file extension will not be removed - so you don't need to get it first and add it back.

For what it's worth, because you are using Now for the date stamp and including seconds, you shouldn't have a duplicate file name problem

Thank you Diane- I will try a couple of these changes and update after testing. Best-
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
V Limiting text length in free text field Outlook VBA and Custom Forms 2
D Limiting email notifications message Using Outlook 1
R Limiting meeting attendees using VBA or an add-in Using Outlook 9
R Limiting Outlook Access to Inside Network Only Exchange Server Administration 1
J Limiting time range dislayed on calender - Outlook 2010 Using Outlook 1
J Filename length Using Outlook 8
R 550 maximum allowed line length is 998 octets, got 1012 Using Outlook 7
L Change length of appointment start time Using Outlook 2
G Change length of phone number in contacts Using Outlook 5
M Increase "Move To" List Length Using Outlook 3
Diane Poremsky Error: 5102. Server. Maximum request length exceeded Using Outlook.com accounts in Outlook 0
A text box length Outlook VBA and Custom Forms 3
C Setting a non-standard default meeting length? Using Outlook 4
P Microsoft.Data.Odbc.OdbcCommand CommandText MAX length. Outlook VBA and Custom Forms 1
D Max. length of MSO MailItem.EntryID Outlook VBA and Custom Forms 6
D Length of the MailItem.EntryID Outlook VBA and Custom Forms 2
T To know new message length Outlook VBA and Custom Forms 3
kburrows Outlook Classic - JPG files are corrupted when opened or saved Using Outlook 3
A Outlook 365 New Appointments All saved to a 365 default calendar on Mac Using Outlook 0
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
N Item cannot be saved because it was modified by another user or window, and, Item could not be moved... Using Outlook 0
e_a_g_l_e_p_i Changing where data .pst is saved to Using Outlook 3
L Outlook saved template function too limited Using Outlook 2
P outlook 2008 search box criteria couldn't be saved Using Outlook 2
L Outlook saved email templates Using Outlook 1
T Pictures degrade each time an Outlook item is edited and re-saved Using Outlook 1
N Macro for attachment saved and combine Outlook VBA and Custom Forms 1
S Importing Ribbons Not Saved Using Outlook 7
Cdub27 Your changes to this item couldn't be saved because Server Denied Operation (HTTP 403 Forbidden) Using Outlook 1
C Changing the name of Outlook Messages saved to a folder Using Outlook 1
R Outlook 2010 - Reading Pane and To Do Bar Settings Not Saved on Exit Using Outlook 2
Jessica .msg file saved in network drive appearing in Deleted Items folder Using Outlook 3
B Saved emails in folders I created on left of screen are being erased as I open each folder Using Outlook 0
P OUTLOOK TEMPLATES ARE NOT SAVED IN SENT FOLDER Using Outlook 1
P Item cannot be saved because it was modified by another user or window Using Outlook 8
bhogesh How to import .htm saved mail to outlook Using Outlook 3
I how to create appointment using saved template onto public folder shared calendar Using Outlook 3
B Configure Outlook so Exchange uses cached mode but new items are by default saved to Outlook.com Using Outlook 1
Hudas Hyperlink Saved Outlook Email to MS Access Table Using Outlook 4
4 VBA - How to retrieve Meetings Saved but Not Sent? Using Outlook 0
F Outlook 2007 is not remembering saved passwords Using Outlook 2
J Changed my "From" email address creates problem with previously saved contacts Using Outlook 20
K External Mtg Invitees have =SMTP: added to email address after mtg saved Using Outlook 5
B Opening a saved Outlook customized form the same folder Using Outlook 2
B Opening a saved Outlook customized form after moving it to another folder Using Outlook 2
D Saved emails not appearing in draft folder Using Outlook 3
G Where are Saved Reports physically stored? BCM (Business Contact Manager) 0
E Sent Emails not being saved Using Outlook.com accounts in Outlook 1
R Outlook Options can not be saved Using Outlook 0
I Outlook 2010 Saved Drafts Time & Date "NONE" - Ideas? Using Outlook 5

Similar threads

Back
Top