c# - _MailAutoSig 书签丢失(Outlook 2010)

标签 c# .net outlook ms-word outlook-addin

我不久前为 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() 事件,然后触发计时器以运行一个函数,该函数将依次访问电子邮件对象并按您想要的方式对其进行操作,添加用户属性标记邮件已处理,然后重新发送邮件。

例如:

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() 事件中才被设置,所以我强制发送电子邮件,获取我的信息,然后取消发送。效果很好。

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

关于c# - _MailAutoSig 书签丢失(Outlook 2010),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5994314/

相关文章:

c# - 编写 C++ .dll 并访问它们 Unity-3D

c# - 在 ASP.NET Core 中覆盖 User.IsInRole

c# - 使用 azure 在桌面应用程序和 Web 应用程序之间共享数据

visual-studio-2010 - 如何实现两个按钮点击事件

c# - (Outlook 2010) 在 C# 中获取邮件 header

c# - 如何使用 Html.RenderAction 实现 ModelBinding?

c# - 在 C# wpf 项目中以编程方式将图像添加到 DataGrid - 如何?

c# - 为什么 Finalize/Destructor 示例在 .NET Core 中不起作用?

.net - 如何将 UWP StorageFile 转换为 .NET FileInfo?

c# - 从 SQL Server 中使用 xp_cmdshell 调用 Outlook COM 库