_MailAutoSig书签缺失(Outlook 2010)
本文关键字:Outlook 2010 MailAutoSig 书签 | 更新日期: 2023-09-27 17:56:16
不久前,我为Outlook编写了一个插件,该插件在Outlook消息中的签名下方添加/删除了可选的标语。 此加载项工作没有问题。
我正在编写第二个加载项,该加载项可能需要在该加载项下方添加信息(无论是否存在可选签名),并再次从 Word 编辑器引用_MailAutoSig书签。 我遇到的问题是此书签似乎不再出现,其他加载项的书签也不再出现。
下面两段代码的一个区别是,第一段代码的 MailItem 是从 ItemSend 传递的对象转换而来的,而第二段代码是在 ItemSend 事件之前处理的。
这是我目前正在编写的代码:
Word.Document toMsg = msg.GetInspector.WordEditor as Word.Document;
foreach (Word.Bookmark b in toMsg.Bookmarks)
Debug.Print(b.ToString());
Word.Range r_toMsg;
try
{
string oBookmark = "_MailAutoSig"; // Outlook internal bookmark for location of the e-mail signature`
object oBookmarkObj = oBookmark;
if (toMsg.Bookmarks.Exists(oBookmark) == true)
Debug.Print("sigbookmark");
r_toMsg = toMsg.Bookmarks.get_Item(ref oBookmarkObj).Range;
}
catch
{
string oOffsiteBookmark = "OffsiteBookmark";
object oOffsiteBookmarkObj = oOffsiteBookmark;
if (toMsg.Bookmarks.Exists(oOffsiteBookmark) == true) // if the custom bookmark exists, remove it
Debug.Print("offsite bookmark");
}
finally
{
r_toMsg = toMsg.Range(missing,missing);
}
这是我的工作加载项中的代码:
void InsertOffsiteSig(Outlook.MailItem oMsg)
{
object oBookmarkName = "_MailAutoSig"; // Outlook internal bookmark for location of the e-mail signature
string oOffsiteBookmark = "OffsiteBookmark"; // bookmark to be created in Outlook for the Offsite tagline
object oOffsiteBookmarkObj = oOffsiteBookmark;
Word.Document SigDoc = oMsg.GetInspector.WordEditor as Word.Document; // edit the message using Word
string bf = oMsg.BodyFormat.ToString(); // determine the message body format (text, html, rtf)
// Go to the e-mail signature bookmark, then set the cursor to the very end of the range.
// This is where we will insert/remove our tagline, and the start of the new range of text
Word.Range r = SigDoc.Bookmarks.get_Item(ref oBookmarkName).Range;
object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
r.Collapse(ref collapseEnd);
string[] taglines = GetRssItem(); // Get tagline information from the RSS XML file and place into an array
// Loop through the array and insert each line of text separated by a newline
foreach (string taglineText in taglines)
r.InsertAfter(taglineText + "'n");
r.InsertAfter("'n");
// Add formatting to HTML/RTF messages
if (bf != "olFormatPlain" && bf != "olFormatUnspecified")
{
SigDoc.Hyperlinks.Add(r, taglines[2]); // turn the link text into a hyperlink
r.Font.Underline = 0; // remove the hyperlink underline
r.Font.Color = Word.WdColor.wdColorGray45; // change all text to Gray45
r.Font.Size = 8; // Change the font size to 8 point
r.Font.Name = "Arial"; // Change the font to Arial
}
r.NoProofing = -1; // turn off spelling/grammar check for this range of text
object range1 = r;
SigDoc.Bookmarks.Add(oOffsiteBookmark, ref range1); // define this range as our custom bookmark
if (bf != "olFormatPlain" && bf != "olFormatUnspecified")
{
// Make the first line BOLD only for HTML/RTF messages
Word.Find f = r.Find;
f.Text = taglines[0];
f.MatchWholeWord = true;
f.Execute();
while (f.Found)
{
r.Font.Bold = -1;
f.Execute();
}
}
else
{
// otherwise turn the plain text hyperlink into an active hyperlink
// this is done here instead of above due to the extra formatting needed for HTML/RTF text
Word.Find f = r.Find;
f.Text = taglines[2];
f.MatchWholeWord = true;
f.Execute();
SigDoc.Hyperlinks.Add(r, taglines[2]);
}
r.NoProofing = -1; // disable spelling/grammar checking on the updated range
r.Collapse(collapseEnd);
}
问题是Microsoft在触发 ItemSend() 事件之前将"Office HTML"(我不确定正确的术语)转换为普通 HTML,这会导致_MailAutoSig书签消失。
取回_MailAutoSig书签的唯一方法是首先取消 ItemSend() 事件,然后触发一个计时器来运行一个函数,该函数将依次访问电子邮件对象并按照您想要的方式操作它,添加一个用户属性来标记电子邮件已被处理,然后再次发送电子邮件。
例如:
将modItem调暗为对象'需要将项目保存在某个位置,以便计时器可以访问它子object_ItemSend(按值项作为对象,取消为布尔值) 如果 Item.UserProperties.Item("isModded") 什么都不是,那么 '用户撰写了一封邮件并点击了"发送",我们需要对签名进行修改 模组项 = 项 取消 = 真 '取消发送,以便我们可以进行修改 我的计时器。启用 = 真 '触发计时器以进行修改 退出子 还 Item.UserProperties.Item("isModded").删除"此标志将防止电子邮件在 ItemSend 和计时器之间乒乓球 结束如果结束子'10毫秒计时器?我认为取消几乎是即时的,但实验子mytimer_Timer() 我的计时器。已启用 = 假 如果不是modItem什么都不是,那么 modItem.HtmlBody = ......签名书签将再次完好无损,因此请进行修改...... modItem.UserProperties.Add("isModded", olText) 'no ping-pong modItem.Send 'Send it Again(发送) 模组项 = 无 结束如果结束子
我必须为一个项目做类似的事情,在我进入 ItemSend() 事件之前,一些 Outlook 字段才被设置,所以我强制电子邮件发送,获取我的信息,然后取消发送。 效果很好。
现在,这是我头顶上写下来的,所以我相信上面的代码不会是完美的,但它应该让你知道需要做什么。