2016-11-15 47 views
0

我正在使用Outlook加载项来处理电子邮件附件,方法是将它们放在服务器上,并将电子邮件地址放入电子邮件中。如何在发送之前更新Outlook邮件正文文本

一个问题是,将URL添加到电子邮件正文的末尾后,用户的光标被重置为电子邮件的开头。

一个相关的问题是,我不知道光标在文本中的位置,所以我无法将我的URL插入到正确的位置。

下面是一些代码,显示我在做什么,为了简单起见,代码假定主体是纯文本。


private void MyAddIn_Startup(object sender, System.EventArgs e) 
    { 

     Application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad); 
    } 

    void Application_ItemLoad(object Item) 
    { 

     currentMailItem = Item as Outlook.MailItem; 

     ((Outlook.ItemEvents_10_Event)currentMailItem).BeforeAttachmentAdd += new Outlook.ItemEvents_10_BeforeAttachmentAddEventHandler(ItemEvents_BeforeAttachmentAdd); 


    } 
void ItemEvents_BeforeAttachmentAdd(Outlook.Attachment attachment, ref bool Cancel) 
    { 
     string url = "A URL"; 
     if (currentMailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML) 
     { 
      // code removed for clarity 
     } 
     else if (currentMailItem.BodyFormat == Outlook.OlBodyFormat.olFormatRichText) 
     { 
      // code removed for clarity 
     } 
     else 
      currentMailItem.Body += attachment.DisplayName + "<" + url + ">"; 

     Cancel = true; 
    } 
+0

http://stackoverflow.com/questions/38433898/c-sharp-outlook-how-can-i-get-the-cursor-position-在主题领域的一个马 – stuartd

回答

0

使用Application.ActiveInspector.WordEditor检索Word文档对象。使用Word对象模型执行所有更改。

+0

谢谢,我只是在这方面看。 –

0

这似乎做什么,我想:

using Microsoft.Office.Interop.Word; 
    void ItemEvents_BeforeAttachmentAdd(Outlook.Attachment attachment, ref bool Cancel) 
    { 
     if (attachment.Type == Outlook.OlAttachmentType.olByValue) 
     { 
      string url = "A URL"; 
      Document doc = currentMailItem.GetInspector.WordEditor; 
      Selection objSel = doc.Windows[1].Selection; 
      object missObj = Type.Missing; 

      doc.Hyperlinks.Add(objSel.Range, url, missObj, missObj, attachment.DisplayName, missObj); 

      Cancel = true; 
     } 
    } 
相关问题