0

I'm working in .NET 3.5, Visual Studio 2010. I'm working on an Outlook Add-In that saves some email into a folder. I've gotten it to work using the Microsoft.Office.Interop.Outlook.MailItem.SaveAs function. However, the file properties have only the current time (time when the file was exported through the Add-In) as their Date Modified/Date Created etc., and other properties such as To, From, CC, BCC are not there.

If you open a folder in Windows Explorer (I'm using Windows 7), go to the top where it says Name, Date Modified, Type, etc., you can click on More, and see other various columns that might be relevant like "Album Artist", "To", "From", etc.

C# has a really easy way to modify the timings, File.SetCreationTime(filename, DateTime object);. However, there's no .SetTo or .SetAlbumArtist or anything like that. How would I go about modifying those properties?

Update 1: through research, I found this link: Read/Write 'Extended' file properties (C#), so that might contain the answer...but I have no idea how. The accepted answer mentions running a method on a shell using a .dll. The second answer contains C# code, a commenter then asked basically what I want to know (how to modify the properties of a particular file), and the next commenter responded with "you can't set these"...so I'm still at square 1.

Update 2: I also tried the following:

foreach (Object selectedObject in explorer.Selection)
{
     Outlook.MailItem email = (selectedObject as Outlook.MailItem);
     //Modify the information about the email
     email.To = "I filled in To";
     email.SaveAs(filename, OlSaveAsType.olMSG);
}

This code successfully grabs the selected email(s) and save them under filename. However, the email.To = "I filled in To" changes the information when you open the .msg, but not the file properties.

Community
  • 1
  • 1
Jake
  • 3,142
  • 4
  • 30
  • 48
  • There's no File.Tag like there's a File.SetCreationTime()...where did you mean I should be able to access it? – Jake Jul 12 '12 at 18:02
  • here is a link that you can look at .. but you will have to convert the code to C# or you can use the VB.NET code and just put in your header using Microsoft.VisualBasic http://p2p.wrox.com/excel-vba/35766-file-information.html – MethodMan Jul 12 '12 at 18:05

1 Answers1

1

This cannot be changed, because it actually is not any file property in the terms of the filesystem (like file creation or modification datetime).

Columns in Windows Explorer you are talking about are "virtual" and they are "only" the feature of Windows Explorer. It "understand" content of some file types and it can handle showing and sorting columns like that.

If you want to change To, From etc. you have to change the content of the file you are saving, i.e. change the To or From in the message.

To do so, if You have an Microsoft.Office.Interop.Outlook.MailItem object (which you are just saving), set desired properties on that object before you save it to file, i.e.:

MailItem mail = ...;
mail.To = "some new to";
mail.Subject = "new subject";
mail.SaveAs(fileToSave, OlSaveAsType.OlMSG);

I don't know if it also changes email stored in the Outlook, if it so, create a copy of the email before changing properties

MailItem copyOfMailToSave = (MailItem)mail.Copy();
  • Ok, interesting. So would the workflow be to open the file, add a header to the top called "To", fill it in with that information, then save the file? – Jake Jul 12 '12 at 18:11
  • I don't know the exact format in which MS Outlook saves emails. But if you can open it in notepad and see line starting with To, From or Subject before main content of the email, you could change these lines. I think that simple putting To line before everything would cause problem when reopening the file in Outlook. – Lukáš Doležal Jul 12 '12 at 18:20
  • Yeah, adding the header didn't work. I also found some interesting information here: http://stackoverflow.com/questions/5337683/how-to-set-extended-file-properties, which suggested that I use a .dll file for a Office Documents, but the standard format from Outlook (.msg) is not considered an Outlook Document so it doesn't work for my purposes. – Jake Jul 12 '12 at 18:30
  • Yeah, I connected to my work remote desktop where I have access to MS Outlook. It saves email in some binary format. And yeah, how to for MS Office Excel/Work will not work, I think. Word or Excel documents have special properties, it's true, but they are specific to "document" like thinks (Author, title..), not email. – Lukáš Doležal Jul 12 '12 at 18:44
  • Look up for my last edit in the answer :) It could be what you want. – Lukáš Doležal Jul 12 '12 at 18:51
  • Nope, didn't work for me. I declare a new mail item from the selected one in Outlook, modify the "To" like you did, then save it using the SaveAs like you did. The 'To' remains unset - see my edit, the code doesn't show up well in comments – Jake Jul 12 '12 at 19:05