Difference between attachment displayname and filename

Status
Not open for further replies.

Van Knowles

New Member
Outlook version
Outlook 2007
Email Account
Exchange Server 2013
Hi. I've looked around the Internet a bit and have seen a lot of examples of Outlook VBA code using Attachment.DisplayName to save email attachments as files. However, my experience is that DisplayName (a) can have characters that are not valid for the Windows file system and (b) DisplayName does not include a file extension when the attachment is another email message (MSG format).

Some limited testing suggests that Attachment.FileName is the better choice because it appears to always be a valid Windows file name and always has an extension.

Does anyone have definitive information about the differences between these two properties of the Attachment object, especially any pitfalls to using FileName?

Thanks!
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Afaik it depends on which method is used to attach the data. The usual attachment (attached by value) has both properties when you create the email but only the file name on the receiver's side. Still, when you access the DisplayName property, Outlook shows data, so I guess it's showing the file name actually.

If it's an embedded attachment, it has a DisplayName but no file name.

I have never seen that one can attach a file (as opposed to adding, for instance, a contact item) and get different values in DisplayName and FileName. Afaik it's the opposite: Outlook doesn't allow to change the DisplayName so that spammers cannot add an exe file and name it 'jpg'.
 

Van Knowles

New Member
Outlook version
Outlook 2007
Email Account
Exchange Server 2013
Just experimenting with different messages and attachments, I found the following:
  1. The DisplayName and FileName are usually the same.
  2. If the attachment is itself an Outlook message (MSG file), then DisplayName shows the subject of the message, which might contain characters not valid for a file name; it also has no extension. FileName shows a valid file name including the MSG extension.
  3. It does not matter whether the attachment is embedded or not. I found a FileName value for both types of attachments. I admit that I did not look at a lot of examples, so maybe there are cases in which there is no FileName value.
My hypothesis is that Outlook always provides a valid file name in the FileName property, but the DisplayName property value depends on the type of attachment and is not always valid as a file name.

What I know is that DisplayName is not always valid as a file name, so I think there are many examples of code (including some provided by Microsoft like this one) that will fail if the attachment is an Outlook message. The resulting name string will have no extension and might have invalid file name characters.

What I do not know for sure is whether FileName always contains a valid file name. I also do not know whether there are other kinds of attachments for which FileName and DisplayName differ.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Did you use OutlookSpy to look at the "real" values? If you attach a file, it certainly will have a valid file name. If you attach an Outlook item not from the file system, but directly from Outlook, then the source is not a file, and in that case I would not suppose Outlook to translate anything into a valid file name, however, it adds the file extension to the file name.

So to be sure, always use the FileName property, and ensure yourself it doesn't contain invalid chars.
 

Van Knowles

New Member
Outlook version
Outlook 2007
Email Account
Exchange Server 2013
I have been using VBA with checkpoints and displaying the names in the immediate window, so I know exactly what the property values are.

When you attach an email message, Outlook does, in fact, translate the message subject into a valid system file name with .MSG extension. I have observed this.

Yes, I think it would be good practice to verify the characters in the FileName property, even though they seem to be valid when I've looked at them.

Which is back to my original question--whether there is any definitive documentation of these properties.
 

Van Knowles

New Member
Outlook version
Outlook 2007
Email Account
Exchange Server 2013
:)

I'm simply saying that the MSDN documentation does not seem to completely describe what is returned by FileName and DisplayName. Which, in my experience, is pretty typical.

I can say definitively that if you use DisplayName as the system file name when saving an attachment, as Microsoft shows in its example code, you will get invalid file names in some situations, and I don't think Microsoft has documented that.
 
Status
Not open for further replies.
Top