将Outlook邮件嵌入word文档

时间:2014-12-01 09:32:24

标签: c# ms-word outlook automation

是否可以通过Outlook Mailitem列表以编程方式将Outlook邮件项目嵌入到word文档中。??

我正在努力实现这样的目标

Word.Application wdApp = new Word.Application();
Word.Document wdDoc = wdApp.Documents.Add(ref missing, ref missing, ref missing,
    ref missing);

foreach(Outlook.MailItem olMail in mailAttachments)
{
   //Paste/embbed this olMail into the word document
}

3 个答案:

答案 0 :(得分:2)

Ya最后我找到了一个有效的解决方案

我使用了InlineShapes.AddOLEObject方法

我的解决方案:

static void creatDocument(List<Outlook.MailItems> mailAttachments)
{

 string userprofile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);  

 object missing = System.Reflection.Missing.Value
 object start=0;
 object end =0;
 object classType ="{00020D0B-0000-0000-C000-000000000046}";     
 object fileName;
 object linkToFile = false;
 object displayAsIcon = true;
 object iconFileName = Path.Combine(userprofile,"Pictures\MailIcon.ico");
 object iconIndex =0;
 object iconLabel;
 object range;

 Word.Application wdApp=new Word.Application();
 Word.Document wdDoc = wdApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

 Range rng = wdDoc.Range(ref start,ref missing);

 foreach(outlook.MailItem olMail in mailAttachments)
 {
   olMail.SaveAs(Path.Combine(userprofile,"Documents\TemperoraySave") + CleanFileName(olMail.Subject) + ".msg" ,Outlook.OlSaveAsType.olMsg);
   fileName = Path.Combine(userprofile,"Documents\TemperoraySave") + CleanFileName(olMail.Subject) + ".msg"
   iconLabel = CleanFIleName(olMail.Subject) + ".msg";

   rng = wdDoc.Content;
   rng.Collapse(WdCollapseDirection.wdCollapseEnd);
   range = rng;

   wdDoc.InLineShapes.AddOLEObject(ref classType,ref fileName,ref linkToFile,ref displayAsIcon,ref iconFIleName,ref iconIndex,ref iconLabel,ref range);

   var mailRanger = wdDoc.Paragraphs.Add();
   mailRanger.Format.SpaceAfter =10f;
 }

}
 private static string CleanFileName(string fileName)
    {
        return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
    }

答案 1 :(得分:0)

不。 Word对象模型不提供任何内容。相反,您可以考虑使用CustomDocumentProperties enter link description here集合来存储自定义数据。例如,您可以将消息另存为.msg文件,并将文件的路径或数据库中记录的ID保存到自定义文档属性。之后,当您需要打开邮件时,您可以获取用于检索电子邮件的ID或路径。

答案 2 :(得分:0)

您无法嵌入电子邮件的源文本,但可以复制MailItem.HTMLBody或MailItem.Body(文本)值并将其插入Word文档。