![](/img/trans.png)
[英]How to open outlook mail object as a draft email before sending?
[英]How to update outlook mail body text before sending
我正在使用Outlook加载项来处理电子邮件附件,方法是将它们放置在服务器上,然后在电子邮件中放置URL。
一个问题是,将URL添加到电子邮件正文的末尾后,用户的光标将重置为电子邮件的开头。
一个相关的问题是我不知道光标在文本中的位置,因此无法将URL插入正确的位置。
这是一些显示我在做什么的代码,为简单起见,该代码假定主体为纯文本。
private void MyAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);
}
void Application_ItemLoad(object Item)
{
currentMailItem = Item as Outlook.MailItem;
((Outlook.ItemEvents_10_Event)currentMailItem).BeforeAttachmentAdd += new Outlook.ItemEvents_10_BeforeAttachmentAddEventHandler(ItemEvents_BeforeAttachmentAdd);
}
void ItemEvents_BeforeAttachmentAdd(Outlook.Attachment attachment, ref bool Cancel)
{
string url = "A URL";
if (currentMailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
{
// code removed for clarity
}
else if (currentMailItem.BodyFormat == Outlook.OlBodyFormat.olFormatRichText)
{
// code removed for clarity
}
else
currentMailItem.Body += attachment.DisplayName + "<" + url + ">";
Cancel = true;
}
使用Application.ActiveInspector.WordEditor
检索Word Document对象。 使用Word对象模型执行所有更改。
这似乎可以满足我的要求:
using Microsoft.Office.Interop.Word;
void ItemEvents_BeforeAttachmentAdd(Outlook.Attachment attachment, ref bool Cancel)
{
if (attachment.Type == Outlook.OlAttachmentType.olByValue)
{
string url = "A URL";
Document doc = currentMailItem.GetInspector.WordEditor;
Selection objSel = doc.Windows[1].Selection;
object missObj = Type.Missing;
doc.Hyperlinks.Add(objSel.Range, url, missObj, missObj, attachment.DisplayName, missObj);
Cancel = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.