范围文本的 Office 插件自定义属性

Office Addin Custom Attributes on Range Text

我想知道是否可以从我的 Word 插件向简单的范围文本或段落文本添加额外的属性。为简单起见,请参阅下面的代码,我可以在其中用文本填充文档,但另外我想存储额外的但在幕后的信息以及该文本。最后,我希望这些文件在处理阶段时使用 Open Xml SDK 读取这些自定义属性。

private void AddAttributedContent(string documentContent)
        {
            var doc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
            var range = doc.Range();
            range.Font.Size = 12;
            range.Font.Name = "Calibri";
            range.Underline = Word.WdUnderline.wdUnderlineSingle;
            range.Text = documentContent;
            // range.AddOpenXmlProperty("MyProp", "MyValue");

            var para = doc.Paragraphs.Add();
            para.Range.Text = documentContent;
            //para.AddCustomProperty("MyProp", "MyVal");

        }

编辑:

理想情况下,我们的 属性 会位于生成的 RunProprties 中:

不,你不能那样做。不过,您可能可以使用内容控件或文本框来完成。

一个ContentControl, you could probably use Tag (if this is like sources, it probably has to be unique, though the description seems to imply it's meant for the purpose you need) or Title.

The Tag property is different from the Title property in that a tag is never displayed while a user edits a document. Instead, developers can use it to store a value for programmatic manipulation while the document is opened.

对于文本框 (a Shape), you could use AlternativeText or Title.

请注意,如果将文档另存为 HTML,更改标题(在这两种情况下)或替代文本可能会影响文档的显示方式。