Ravi
Ravi

Reputation: 49

C# get the selected text in the body of the email for MS Outlook 2007

I am using C# and outlook-addin.

I want to be able to have the user select a part of the mail message/body and I want to be able to copy the selected items, and it store it to an sql server database.

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);
}

On the mailITem.Body, I just want to paste the selected text/images of the user from the selected mail (the currently opened email).

I cant find the paste method, How can I implement it?

Upvotes: 1

Views: 3330

Answers (1)

hmk
hmk

Reputation: 969

Outlook cannot get the selected text in the mail body, so convert the outlook to word editor, so you can follow these 3 steps:

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

first add the dll reference

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;
}

Upvotes: 4

Related Questions