-->

How do I get the selected text from a WordEditor O

2019-07-12 02:28发布

问题:

i want to Copy selected Text from WordEditor(Body of Outlook Email) to another Form when i searched i found that part of code to Copy to new Word Document by using Microsoft.Office.Interop.Word.Document

Outlook.MailItem mailItem;
Outlook.Inspector inspector = mailItem.GetInspector;

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

// Copy the selected objects
document.Application.Selection.Copy();

i always get Error with mailItem.GetInspector part ( Object refrence is not set to instance of object )

回答1:

You need to request the active inspector from the Application object. Globals.ThisAddIn.Application.ActiveInspector() will provide you with the currently active inspector reference which you can then request the CurrentItem and convert to a MailItem reference if the item is a MailItem type (could also be a CalendarItem, TaskItem, NoteItem, etc.).



回答2:

for starters you are not using the OutLook.MailItem properly.. use something like this as a test and utilize it and make changes as you see fit to fit your UseCase

public void ShowEmail(string To, string Subject, string Body)
{
    Outlook.Application myOutlook = new Outlook.Application();
    Outlook.NameSpace myNamespace = myOutlook.GetNamespace("MAPI");
    myNamespace.Logon(null, null, null, null);
    Outlook.MAPIFolder outbox = myNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
    Outlook.MailItem mail = (Outlook.MailItem)outbox.Items.Add(Outlook.OlItemType.olMailItem);

    mail.Recipients.Add(To);
    mail.Subject = Subject;
    mail.Body = Body;

    mail.GetInspector.Activate();
}

Go ahead and test it, create a button on your form and in the Click event handler:

private void button1_Click(object sender, EventArgs e)
{
    ShowEmail("youremailOutlookAddress.com", "Hello!", "Hey here's a test Email!");
}

OutLookMailItem how to use Outlook



回答3:

Set the MailItem object to "objExplorer.Selection[1] as Microsoft.Office.Interop.Outlook.MailItem;" where objExplorer = AddIn name.Globals.ThisAddIn.Application.ActiveExplorer();