【问题标题】:Embbed outlook mails into word document将 Outlook 邮件嵌入 Word 文档
【发布时间】:2015-01-29 06:40:37
【问题描述】:

有没有办法以编程方式将 Outlook 邮件项从 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
}

【问题讨论】:

    标签: c# ms-word outlook automation


    【解决方案1】:

    是的,我终于找到了一个有效的解决方案

    我使用了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));
        }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-30
          • 2021-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-26
          • 1970-01-01
          • 2011-05-21
          相关资源
          最近更新 更多