DocumentItem.SaveAs results in corrupted file

[亡魂溺海] 提交于 2019-12-11 00:43:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!