【问题标题】:Append to meeting invite body with or without Redemption附加到带有或不带有兑换的会议邀请正文
【发布时间】: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 中看不到。

我尝试过的事情:

  1. 保存 RDOAppointmentItem
  2. 还将内容添加到 Body 属性中
  3. 使用 SafeAppointmentItem 代替 RDOAppointmentItem;不起作用,因为 HTMLBody 是那里的只读属性
  4. 通过 RDOAppointment.Fields 设置 PR_HTML
  5. 通过 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


    【解决方案1】:

    由于约会正在显示,使用 Word 对象模型 - Inspector.WordEditor 返回 Document Word 对象。

    【讨论】:

    • 嗨 Dmitry,我尝试使用 WordEditor 但总是得到“命令失败”COM 异常。鉴于我在字符串中有 HTML sn-p,我将如何使用 WordEditor 将 HTML 插入到文档中?
    • 请出示您的代码。您可以使用 Region.InsertFile 插入 HTML 内容。
    • 对不起,我忘了写我编辑了我的问题以显示代码。它位于“尝试使用 WordEditor”部分。我试图使用 PasteSpecial 或 PasteAndFormat,但它们都失败了 HTML 内容。这是否意味着只有 InsertFile 可以工作?
    • 我没有使用 PasteSpecial(覆盖键盘内容是一种不好的做法),但是 InsertFile 确实有效 - 我已经多次使用它。
    • 我使用 InsertFile 让它工作了,谢谢!这不是一个很好的解决方案,将 HTML sn-p 写入临时文件然后插入。但正如你所说,两者都没有使用系统剪贴板。
    【解决方案2】:

    根据 Dmitrys 的建议,这是一个可行的解决方案:

    1. 在检查器窗口中显示插入的内容。
    2. 在链接和格式方面正确处理 HTML 内容(只要您保持在 Words HTML 引擎的有限功能范围内)。
    using System;
    using System.IO;
    using System.Text;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace VSTO.AppendHtmlExample
    {
        public class MyExample
        {
            public void AppendAsHTMLViaFile(string content)
            {
                // TODO: Remember to release COM objects range and wordDoc and delete output file in a finally clause
                Outlook.Inspector inspector = ...;
                string outputFolderPath = ...;
                string outputFilePath = Path.Combine(outputFolderPath, "append.html");
    
                Word.Document wordDoc = inspector.WordEditor;
                File.WriteAllText(outputFilePath, $"<html><head><meta charset='utf-8'/></head><body>{content}</body></html>", Encoding.UTF8);
    
                Word.Range range = wordDoc.Content;
                range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    
                object confirmConversions = false;
                object link = false;
                object attachment = false;
    
                range.InsertFile(fileName,
                    ConfirmConversions: ref confirmConversions,
                    Link: ref link,
                    Attachment: ref attachment);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 2018-09-11
      相关资源
      最近更新 更多