2012-02-07 112 views
0

我正在使用C#和outlook-addin。C#在MS Outlook 2007中获取电子邮件正文中的选定文本

我希望能够让用户选择邮件消息/正文的一部分,我希望能够复制选定的项目,并将其存储到SQL Server数据库。

Outlook.Application myApplication = Globals.ThisAddIn.Application; 
Outlook.Explorer myActiveExplorer = 
    (Outlook.Explorer)myApplication.ActiveExplorer(); 

Outlook.Selection selection = myActiveExplorer.Selection; 

if (selection.Count == 1 && selection[1] is Outlook.MailItem) 
{ 
    Outlook.MailItem mail = (Outlook.MailItem)selection[1]; 

    mail.Copy(); // currently opened mail 

    Outlook.MailItem mailItem = (Outlook.MailItem) 
     myApplication.CreateItem(
     Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 

    mailItem.Subject = mail.Subject; 
    mailItem.To = mail.To; 

    mailItem.Body = ?????   // copy only selected text/images of user 

    mailItem.Importance = Outlook.OlImportance.olImportanceLow; 
    mailItem.Display(true); 
} 

mailITem.Body,我只是想给用户的选择的文本/图片来自所选邮件(当前打开的电子邮件)粘贴。

我找不到粘贴方法,我该如何实现它?

回答

3

Outlook无法获得邮件正文选定的文本,所以后市转换为Word编辑器,这样你就可以按照以下三个步骤:

1. get the mail total body 
2. use the word editor based on the **microsoft.office.Interop.word** dll 
3. select the text and to store the any string 

先添加dll引用

object oItem; 
Outlook.Application oApp=new Outlook.Application(); 
Outlook.Explorer oExp=oApp.ActiveExplorer(); 
Outlook.Selection oSel= oExp.Selection; 
for (i = 1; i <= oSel.Count; i++) 
{ 
    oItem = oSel[i]; 
    Outlook.MailItem oMail = (Outlook.MailItem)oItem; 
    Outlook.Inspector inspector = oMail.GetInspector; 

    // Obtain the Word.Document object from the Inspector object 
    Microsoft.Office.Interop.Word.Document document = 
     (Microsoft.Office.Interop.Word.Document)inspector.WordEditor; 

    mailItem.Body = document.Application.Selection.Text; 
} 
相关问题