Word VSTO - 为什么 paraId 有时会丢失

Word VSTO - Why paraId is sometimes missing

我正在从 openXML 中检索段落样式名称并使用 paraID 属性 获取正确的样式名称。

在测试过程中,我注意到根据 word 文档,属性可能不会出现在任何段落中。

我知道这是 Word 的内部运行时段落 ID。

所以问题是:是什么在段落上生成 paraId 属性,似乎无法弄清楚。

ps。我不想使用 get_style() 因为那太懒了...

编辑:添加了代码示例

This is in some documents "0" and in some documents a valid hex id

string sParaId = range.Paragraphs.First.ParaID.ToString("x").ToUpper();

This open xml document sometimes have valid w14:paraId-attribute and sometimes it is missing:

activeDocument = Globals.ThisAddIn.Application.ActiveDocument;
wordXML = XElement.Parse(activeDocument.WordOpenXML);

...我想从 WordOpenXML 中获取类似这样的样式名称,但现在看来我可能会选择其他选项,因为我不知道何时将 paraId 添加到XML.

paraEl = ooXMLElementList.Descendants().Where(x => x.Name.LocalName == 
"p").FirstOrDefault(x => x.Attribute(w14 + "paraId")?.Value == sParaId);

styleName = paraEl.Descendants().FirstOrDefault(x => x.Name.LocalName == 
"pStyle") != null ? paraEl.Descendants().FirstOrDefault(x => 
x.Name.LocalName == "pStyle").Attribute(w + "val").Value : "Normal";

来自 Paragraph.ID 的单词语言参考:

Returns or sets the identifying label for the specified object when the current document is saved as a Web page.

由于文档未另存为 HTML,因此 属性 没有任何意义。

ParaId property 不公开给开发者使用。它在 VBA 对象模型中不可见,但由于 PIA(主要互操作程序集)的生成方式,.NET 开发人员将看到它。来自语言参考:

Reserved for internal use.

不确定您真正想要做什么,但您可以使用 Word 的 Range.Find 功能来搜索格式(样式)。