How do I get the selected text from a WordEditor Object (in Outlook2010) and Copy it to another Form?

て烟熏妆下的殇ゞ 提交于 2019-12-12 18:04:02

问题


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



来源:https://stackoverflow.com/questions/8901066/how-do-i-get-the-selected-text-from-a-wordeditor-object-in-outlook2010-and-cop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!