问题
Within outlook I have various DocumentItems in folders such as the inbox and I am trying to save these to the file system within a drag/drop event.
Here is the code:
for (var i = 1; i <= _application.ActiveExplorer().Selection.Count; i++)
{
var temp = _application.ActiveExplorer().Selection[i];
var documentItem = (temp as DocumentItem);
if (documentItem == null)
continue;
var tempFileName = Path.GetTempPath() + documentItem.Subject;
documentItem.SaveAs(tempFileName);
}
They seem to save successfully and have file sizes:
But when I try to open any of them they all say they cannot be opened so they are corrupted somehow, does anyone have any ideas?
回答1:
You are calling SaveAs without specifying the format, and Outlook Object Model defaults it to olMsg. You end up with an MSG file with a JPG extension.
What you need to do is loop though all attachments in the DocumentItem.Attachments collection and call Attachment.SaveAsFile. You might also want to use the Attachmeent.FileName property.
Just a general comment - multiple dot notation is evil, especially in a loo:
Selection selection = _application.ActiveExplorer().Selection;
for (var i = 1; i <= selection.Count; i++)
{
var temp = selection[i];
var documentItem = (temp as DocumentItem);
...
来源:https://stackoverflow.com/questions/35599752/documentitem-saveas-results-in-corrupted-file