c# - 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() 后,它仍然不显示替换
解决方案
找出原因:如果您尝试(例如在 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 包含
现在它起作用了。
推荐阅读
- javascript - 如何使用 sinnon 从书架 js 模拟回调函数
- node.js - 使用 NPM 脚本在 CLI 参数中设置正则表达式模式时出现“(”意外错误
- node.js - NodeJs 头盔 hsts 配置
- java - 如何使用 Jersey POST 请求发送标头和正文?
- python - Pytorch 不会通过迭代张量构造进行反向传播
- sql - 如何使用 hive 获取列中变量的增量值
- sql - 自动机制在我的桌面上打开 Oracle SQL developer,每周一早上 01:00 am 并在其中运行 sql 查询
- sql - 生成月报表状态的 SQL 查询
- automation - 期望抛出“AssertionError:预期 { Object (browser_, then, ...) } to equal true”
- android - 自定义 Lint 不会在 Android Studio 中突出显示