【发布时间】:2021-06-03 03:08:12
【问题描述】:
我们正在开发 Outlook VSTO 加载项。
现在我正在尝试将一些信息附加到用户正在撰写的会议邀请中。我希望内容出现在正文中,就像单击 Teams-meeting 按钮所做的那样,将格式化的文本和链接附加到正文的末尾。
由于内容是 HTML 并且 Outlook 对象模型不公开 AppointmentItems 的 HTMLBody 属性,我尝试通过 Redemption 设置它:
// Dispose logic left out for clarity but everything except outlookApplication and outlookAppointment is disposed after use
Application outlookApplication = ...;
AppointmentItem outlookAppointment = ...; // taken from the open inspector
NameSpace outlookSession = outlookApplication.Session;
RDOSession redemptionSession = RedemptionLoader.new_RDOSession();
redemptionSession.MAPIOBJECT = outlookSession.MAPIOBJECT;
var rdoAppointment = (RDOAppointmentItem)redemptionSession.GetRDOObjectFromOutlookObject(outlookAppointment);
string newBody = transform(rdoAppointment.HTMLBody); // appends content right before HTML </body> tag
rdoAppointment.BodyFormat = (int)OlBodyFormat.olFormatHTML;
rdoAppointment.HTMLBody = newBody;
问题
Outlook 检查器窗口未使用附加内容进行更新。如果我再次尝试运行代码,我可以在调试器中看到附加的内容,但在 Outlook 中看不到。
我尝试过的事情:
- 保存 RDOAppointmentItem
- 还将内容添加到 Body 属性中
- 使用 SafeAppointmentItem 代替 RDOAppointmentItem;不起作用,因为 HTMLBody 是那里的只读属性
- 通过 RDOAppointment.Fields 设置 PR_HTML
- 通过 WordEditor 粘贴 HTML(见下文)
尝试使用 WordEditor
根据建议,我还尝试通过 WordEditor 插入 HTML:
// Dispose logic left out for clarity but everything except inspector is disposed after use
string htmlSnippet = ...;
Clipboard.SetText(htmlSnippet, TextDataFormat.Html);
Inspector inspector = ...;
Document wordDoc = inspector.WordEditor;
Range range = wordDoc.Content;
range.Collapse(WdCollapseDirection.wdCollapseEnd);
object placement = WdOLEPlacement.wdInLine;
object dataType = WdPasteDataType.wdPasteHTML;
range.PasteSpecial(Placement: ref placement, DataType: ref dataType);
...但我只是收到错误System.Runtime.InteropServices.COMException (0x800A1066): Kommandoen lykkedes ikke.(=“命令失败”)。
我也尝试使用 PasteAndFormat,而不是 PasteSpecial:
range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
...但这也给了System.Runtime.InteropServices.COMException (0x800A1066): Kommandoen lykkedes ikke.。
我在这里做错了什么?
编辑:如果我使用Clipboard.SetText(htmlSnippet, TextDataFormat.Text);,然后使用普通的range.Paste();,HTML 会按预期插入到文档的末尾(但 HTML 元素是按字面插入的,所以没用)。所以一般的方法似乎没问题,我似乎无法让 Outlook / Word 翻译 HTML。
版本信息
Outlook 365 MSO 32 位 救赎5.26
【问题讨论】:
标签: vsto outlook-addin outlook-redemption