首页 > 解决方案 > Outlook 插件:尝试使用 WordEditor 从 RTFBody 中删除文本

问题描述

我试图从 Outlook.Appointment 的 RTFBody 中删除一个 url。我为此使用此代码:

            byte[] rtfBody = myAppointment.RTFBody;
            if (rtfBody.Length > 0)
            {
                if (myAppointment == null)
                {
                    return;
                }
                Outlook.Inspector myInspector = myAppointment.GetInspector;
                Microsoft.Office.Interop.Word.Document document = myInspector.WordEditor;
                var findObject = document.Application.Selection.Find;
                findObject.ClearFormatting();
                findObject.Text = url;
                findObject.Replacement.ClearFormatting();
                findObject.Replacement.Text = "";

                object replaceAll = WdReplace.wdReplaceAll;
                findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref replaceAll, ref missing, ref missing, ref missing, ref missing);

不幸的是,该代码不会将 URL 替换为 RTFBody。有什么遗漏吗?会不会是 URL 中包含的一些特殊字符(如“:”、“/”)导致了这个问题?

在约会项目上调用 Save() 后,它仍然不显示替换

标签: c#outlookms-wordoutlook-addin

解决方案


找出原因:如果您尝试(例如在 Word 中)查找和替换包含超链接的文本(作为 Word 文档中的超链接对象),这将(由于某种原因不起作用)。因此,我在搜索和替换代码之前添加了以下代码(见上文): Microsoft.Office.Interop.Word.Hyperlinks links = document.Hyperlinks;

                foreach (Hyperlink link in links)
                {
                    string c = link.TextToDisplay; // perhaps concatenating Address and SubAddress would be better
                    if (c != null)
                    {
                        if (c.Equals(myUrl))
                        {
                            link.Delete();
                        }
                    }
                }

为了澄清这一点: URL 包含整个文本块,其内容如下

“bla bla bla https://something.com/blabla

而 myUrl 包含

https://something.com/blabla

现在它起作用了。


推荐阅读