DocumentItem.SaveAs保存文档但文件损坏

时间:2016-06-13 23:43:33

标签: c# outlook-addin

我正在编写Outlook添加。我的应用程序获取对项目的引用并将其保存为:

string myFileName = "A.docx";
if (myItem is DocumentItem)
{
   var docItem = myItem as DocumentItem;
   docItem.SaveAs(myFileName);
}

这样可以保护doc doc" A.docx"到我机器上的临时文件夹。

但是,当我双击该文件打开它时,我会弹出一个告诉我该文件已损坏且无法打开的弹出窗口。单击弹出窗口上的确定以恢复文件,如果我选择这样做,则恢复并打开文件。

这让我相信由于某种原因,DocumentItem.SaveAs()没有按预期工作,并且它将文件保存在损坏的状态。

知道上面的代码有什么问题吗?

更新和解决方案: 我想通了(感谢这篇文章DocumentItem.SaveAs results in corrupted file)。由于这篇文章没有详细解释如何做到这一点,我在这里发布我的解决方案,万一有人需要它。

以下是我在上面的帖子中提出的建议:

string myFileName = "A.docx";
if (myItem is DocumentItem)
{
   var docItem = myItem as DocumentItem;
   for (var i = 1; i <= docItem.Attachments.Count; i++)
   {
      var attachment = docItem.Attachments[i];
      attachment.SafeAsFile(myFileName);
   }
}

这解决了腐败问题。非常感谢@DmitryStreblechenko

2 个答案:

答案 0 :(得分:1)

来自DocumentItem.SaveAs的文档:

  

将Microsoft Outlook项目保存到指定的路径并采用指定文件类型的格式。 如果未指定文件类型,则使用MSG格式(.msg)。

因此,除非您指定类型,否则应使用扩展名.msg。如果要保存为Word文档格式,则必须从OlSaveAsType枚举中指定正确的类型。请注意,如果您指定.doc,则扩展名为olDoc

string myFileName = "A.doc";
...
docItem.SaveAs(myFileName, OlSaveAsType.olDoc)

答案 1 :(得分:1)

我想通了(感谢这篇文章DocumentItem.SaveAs导致文件损坏)。由于这篇文章没有详细解释如何做到这一点,我在这里发布我的解决方案,万一有人需要它。

以下是我在上面的帖子中提出的建议:

string myFileName = "A.docx";
if (myItem is DocumentItem)
{
   var docItem = myItem as DocumentItem;
   for (var i = 1; i <= docItem.Attachments.Count; i++)
   {
      var attachment = docItem.Attachments[i];
      attachment.SafeAsFile(myFileName);
   }
}

这解决了腐败问题。非常感谢@DmitryStreblechenko