_MailAutoSig 书签丢失(Outlook 2010)

发布于 2024-11-07 02:14:00 字数 4079 浏览 2 评论 0原文

我不久前为 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);
}

I wrote an addin a while back for Outlook that adds/removes an optional tagline below the signature in an outlook message. This add-in works with no issues.

I'm writing a second add-in that needs to potentially add information below that (whether or not the optional signature is there) and am again referencing the _MailAutoSig bookmark from the Word editor. The issue I'm running into is that this bookmark no longer seems to appear, nor does the bookmark from my other add-in.

One difference in the two pieces of code below is that the first one has the MailItem being converted from an object passed by ItemSend, whereas the second is processed BEFORE the ItemSend event.

Here is the code from what I am currently writing:

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

and here is code from my working add-in:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2024-11-14 02:14:00

问题在于,Microsoft 在触发 ItemSend() 事件之前将“Office HTML”(我不确定正确的术语)转换为普通 HTML,这会导致 _MailAutoSig 书签消失。

恢复 _MailAutoSig 书签的唯一方法是首先取消 ItemSend() 事件,然后触发计时器来运行一个函数,该函数将依次访问电子邮件对象并按照您想要的方式操作它,添加一个用户属性来标记电子邮件已处理完毕,然后重新发送电子邮件。

例如:

Dim modItem As Object 'need to hold the item somewhere so the timer can access it

Sub object_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If Item.UserProperties.Item("isModded") Is Nothing Then
        'User has composed a mail and hit "Send", we need to make our modifications to the signature though
        modItem = item
        Cancel = True 'cancel the Send so we can make the modifications
        mytimer.Enabled = True 'fire off a timer to make the modifications
        Exit Sub
    Else
        Item.UserProperties.Item("isModded").Delete 'this flag will keep the email from ping-ponging between ItemSend and the timer
    End If
End Sub

'10 millisecond timer? I think the cancel is almost instant, but experiment
Sub mytimer_Timer()
    mytimer.Enabled = False
    If Not modItem Is Nothing Then
        modItem.HtmlBody = ...... the signature bookmark will be intact again, so make your modifications ......
        modItem.UserProperties.Add("isModded", olText) 'no ping-pong
        modItem.Send 'send it again
        modItem = Nothing
    End If
End Sub

我必须对一个项目执行类似的操作,其中一些 Outlook 字段直到我处于 ItemSend() 事件中才设置,因此我强制发送电子邮件,获取我的信息,然后取消发送。效果很好。

现在,这是我的想法,所以我确信上面的代码不会是完美的,但它应该让您了解需要做什么。

The problem is that Microsoft converts the "Office HTML" (I'm not sure the proper term) to normal HTML before it fires the ItemSend() event, which causes the _MailAutoSig bookmark to disappear.

The only way to get the _MailAutoSig bookmark back is to first CANCEL the ItemSend() event, then fire off a timer to run a function which will in turn access the email object and manipulate it how you want, add a user property to mark the email has been processed, and then send the email again.

For example:

Dim modItem As Object 'need to hold the item somewhere so the timer can access it

Sub object_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If Item.UserProperties.Item("isModded") Is Nothing Then
        'User has composed a mail and hit "Send", we need to make our modifications to the signature though
        modItem = item
        Cancel = True 'cancel the Send so we can make the modifications
        mytimer.Enabled = True 'fire off a timer to make the modifications
        Exit Sub
    Else
        Item.UserProperties.Item("isModded").Delete 'this flag will keep the email from ping-ponging between ItemSend and the timer
    End If
End Sub

'10 millisecond timer? I think the cancel is almost instant, but experiment
Sub mytimer_Timer()
    mytimer.Enabled = False
    If Not modItem Is Nothing Then
        modItem.HtmlBody = ...... the signature bookmark will be intact again, so make your modifications ......
        modItem.UserProperties.Add("isModded", olText) 'no ping-pong
        modItem.Send 'send it again
        modItem = Nothing
    End If
End Sub

I had to do something similar for a project where some Outlook fields were not set until I was in the ItemSend() event, so I was forcing the email to send, get my information, then cancel the send. It worked great.

Now, this was written off the top of my head, so I'm sure the code above will not be perfect, but it should give you the idea of what needs to be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文