我想在搜索时将所选文本从WordEditor(Outlook电子邮件正文)复制到另一个表单我发现使用 Microsoft.Office.Interop.Word.Document <复制到新Word文档的部分代码/强>
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();
我总是得到mailItem.GetInspector部分的错误(对象引用未设置为对象的实例)
答案 0 :(得分:3)
您需要从Application
对象请求活动检查器。 Globals.ThisAddIn.Application.ActiveInspector()
将为您提供当前有效的检查员参考,然后您可以请求CurrentItem
并转换为MailItem
参考,如果该项为MailItem
类型(也可以是CalendarItem,TaskItem,NoteItem等。)。
答案 1 :(得分:2)
对于初学者你没有正确使用OutLook.MailItem .. 使用类似的东西作为测试并使用它并根据您的需要进行更改以适合您的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如何使用Outlook
答案 2 :(得分:0)
将MailItem对象设置为“objExplorer.Selection [1] as Microsoft.Office.Interop.Outlook.MailItem;”其中objExplorer = AddIn name.Globals.ThisAddIn.Application.ActiveExplorer();