Word重复节内容控件XML映射问题:加命名空间仅显示首条数据
Word 2013 带命名空间XML与重复节内容控件映射异常问题
研究Word 2013及以上版本中XML与wdContentControlRepeatingSection类型内容控件的映射机制时,发现以下异常现象:
- 微软官方示例代码可正常将三条书籍数据填充至表格
- 为XML添加命名空间后,表格仅显示第一本书籍数据
经检查,XML结构及XPath表达式均合法有效,怀疑这是Word 2013的Bug(暂未在更高版本Word中测试)。
正常运行的示例代码
Sub testRepeatingSectionControl() Dim objRange As Range Dim objTable As Table Dim objCustomPart As CustomXMLPart Dim objCC As ContentControl Dim objCustomNode As CustomXMLNode Dim wdDoc As Word.Document Dim bResult As Boolean Set wdDoc = Application.Documents.Add Set objCustomPart = ActiveDocument.CustomXMLParts.Add objCustomPart.LoadXML ("<books>" & _ "<book><title>Everyday Italian</title>" & _ "<author>Giada De Laurentiis</author></book>" & _ "<book><title>Harry Potter</title>" & _ "<author>J K. Rowling</author></book>" & _ "<book><title>Learning XML</title>" & _ "<author>Erik T. Ray</author></book></books>") Set objRange = ActiveDocument.Paragraphs(1).Range Set objTable = ActiveDocument.Tables.Add(objRange, 2, 2) With objTable.Borders .InsideLineStyle = wdLineStyleSingle .OutsideLineStyle = wdLineStyleDouble End With Set objRange = objTable.Cell(1, 1).Range Set objCustomNode = objCustomPart.SelectSingleNode("/books[1]/book[1]/title[1]") Set objCC = ActiveDocument.ContentControls.Add(wdContentControlText, objRange) bResult = objCC.XMLMapping.SetMappingByNode(objCustomNode) Debug.Print bResult Set objRange = objTable.Cell(1, 2).Range Set objCustomNode = objCustomPart.SelectSingleNode("/books[1]/book[1]/author[1]") Set objCC = ActiveDocument.ContentControls.Add(wdContentControlText, objRange) bResult = objCC.XMLMapping.SetMappingByNode(objCustomNode) Debug.Print bResult Set objRange = objTable.Rows(1).Range Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRepeatingSection, objRange) bResult = objCC.XMLMapping.SetMapping("/books[1]/book") Debug.Print bResult End Sub
添加命名空间后异常的代码
Sub testRepeatingSectionControl2() Dim objRange As Range Dim objTable As Table Dim objCustomPart As CustomXMLPart Dim objCC As ContentControl Dim objCustomNode As CustomXMLNode Dim bResult As Boolean Dim wdDoc As Word.Document Set wdDoc = Application.Documents.Add Set objCustomPart = wdDoc.CustomXMLParts.Add objCustomPart.LoadXML ("<b:books xmlns:b='urn:example.com/bib'>" & _ "<b:book><b:title>Everyday Italian</b:title>" & _ "<b:author>Giada De Laurentiis</b:author></b:book>" & _ "<b:book><b:title>Harry Potter</b:title>" & _ "<b:author>J K. Rowling</b:author></b:book>" & _ "<b:book><b:title>Learning XML</b:title>" & _ "<b:author>Erik T. Ray</b:author></b:book>" & _ "</b:books>") Call objCustomPart.namespaceManager.AddNamespace("b", "urn:example.com/bib") Set objRange = wdDoc.Paragraphs(1).Range Set objTable = wdDoc.Tables.Add(objRange, 2, 2) With objTable.Borders .InsideLineStyle = wdLineStyleSingle .OutsideLineStyle = wdLineStyleDouble End With Set objRange = objTable.Cell(1, 1).Range Set objCustomNode = objCustomPart.SelectSingleNode("/b:books[1]/b:book[1]/b:title[1]") Set objCC = wdDoc.ContentControls.Add(wdContentControlText, objRange) bResult = objCC.XMLMapping.SetMappingByNode(objCustomNode) Debug.Print bResult Set objRange = objTable.Cell(1, 2).Range Set objCustomNode = objCustomPart.SelectSingleNode("/b:books[1]/b:book[1]/b:author[1]") Set objCC = wdDoc.ContentControls.Add(wdContentControlText, objRange) bResult = objCC.XMLMapping.SetMappingByNode(objCustomNode) Debug.Print bResult Set objRange = objTable.Rows(1).Range Set objCC = wdDoc.ContentControls.Add(wdContentControlRepeatingSection, objRange) bResult = objCC.XMLMapping.SetMapping("/b:books[1]/b:book") Debug.Print bResult End Sub
问题分析
在Word 2013环境下,即使已通过namespaceManager.AddNamespace正确注册命名空间,且XPath表达式能正常查询到目标节点,wdContentControlRepeatingSection控件仍无法正确遍历带命名空间的重复节点集合,仅渲染第一个匹配项。该现象大概率是Word 2013的版本Bug,建议在Word 2016及以上版本中进行兼容性测试验证。
参考Stack Exchange上的相关讨论:「Word 2013:VBA插入映射到现有自定义XML的内容控件」
内容的提问来源于stack exchange,提问作者Peter M.
相关产品推荐
相关产品推荐

