我使用 VSTO 和 C# 开发了一个 Outlook 插件。
第 1 步:我为约会探索创建了一个功能区,当单击功能区按钮时,读取.rtf
文件并将.rtf
文件插入到约会正文中。
try
{
Outlook.Inspector insp = Globals.ThisAddIn.Application.ActiveInspector();
Outlook.AppointmentItem meetingItem = insp.CurrentItem() as Outlook.AppointmentItem;
if(meetingItem == null){
MessageBox.Show("the meeting is null,create a meeting Item");
meetingItem = (Outlook.AppointmentItem)insp.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
}
if(meetingItem != null) {
meetingItem.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
object missing = System.Reflection.Missing.Value;
Word.Application wordAppl;
Word.Document wordDoc;
Word.Selection wordSel = null;
wordDoc = (Word.Document)insp.WordEditor;
wordAppl = wordDoc.Parent as Word.Application;
wordDoc.Activate();
String path = "D://WordSendTest//template//en_US.rtf";
String fileName = path.ToString();
wordSel = (Word.Selection)wordAppl.Selection;
wordSel.Range.Delete(ref missing, ref missing);
object falseRef = false;
wordSel.Range.InsertFile(fileName, ref missing, ref falseRef, ref falseRef, ref falseRef);
}
}
catch (Exception ex)
{
MessageBox.Show("Robbin click error!"+ ex.ToString());
}
第 2 步:当用户单击“发送按钮”时,捕获 ItemSend 事件。
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
第 3 步:在电子邮件正文的末尾插入一些“字符串文本”。
void Application_ItemSend(object Item, ref bool Cancel)
{
Outlook.Inspector insp = Globals.ThisAddIn.Application.ActiveInspector();
Outlook.AppointmentItem meeting = insp.CurrentItem as Outlook.AppointmentItem;
Word.Document wordDoc = insp.WordEditor as Word.Document;
Word.Application wordApp = wordDoc.Parent as Word.Application;
Word.Selection wordSel =(Word.Selection) wordApp.Selection;
object missing = System.Reflection.Missing.Value;
object units = Microsoft.Office.Interop.Word.WdUnits.wdStory;
object findEnd = (object)"~-----~~-----~-----~-----~-----~-----~\r";
object wholeWord = true;
wordSel.HomeKey(ref units, ref missing);
if(wordSel.Find.Execute(ref findEnd,ref missing,ref wholeWord,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing))
{
wordSel.MoveUp(ref missing,ref missing,ref missing);
wordSel.TypeText("--=Head for Insert info =--\n");
wordSel.TypeText("when user click the <send button>,the Tex info will be insert and show here \n");
wordSel.TypeText("--=End for Insert info=--\n");
}
else {
MessageBox.Show("not fonud the END tag");
}
}
问题:在调试模型中,单击发送按钮时,添加文本成功插入正文。但是,实际发出的邮件并没有改变,只是在点击“发送按钮”之前相同。当我在日历中打开约会时,正文没问题,文本已成功插入邮件正文的末尾。
那么,谁知道为什么?为什么邮件没有与更新正文一起发送,而是更新邮件正文保存在日历中。