VBA Word CustomXMLPart 中在指定的Node"前添加Node
来源:互联网 发布:工商银行数据中心 编辑:程序博客网 时间:2024/05/16 16:23
网上我看见有人想用InsertNodeBefore来实现这个想法。代码如下:
Sub HowDoesInsertNodeBeforeWork()Dim oCXPart As CustomXMLPartDim oCXNode As CustomXMLNodeDim strXML As StringstrXML = "<?xml version='1.0' ?><invoice xmlns='http://abc...xyz'>" _ & "<items>" _ & "<item ><name supplier='Home Depot'>Hammer</name><price>12</price></item>" _ & "<item ><name supplier='Lowes'>Hammer</name><price>12</price></item>" _ & "<item ><name supplier='Contoso'>Hammer</name><price>11</price></item>" _ & "</items>" _ & "</invoice>" On Error Resume Next ActiveDocument.CustomXMLParts(4).Delete On Error GoTo 0 'Add the part. Set oCXPart = ActiveDocument.CustomXMLParts.Add 'Load the XML oCXPart.LoadXML strXML 'Define a node. Set oCXNode = oCXPart.SelectSingleNode("//*[@supplier='Contoso']") Debug.Print oCXNode.BaseName 'Attempt to insert a node before the defined node. oCXNode.InsertNodeBefore "Test", , msoCustomXMLNodeElement, "Test Node Text" Debug.Print oCXNode.ParentNode.XML 'Seems the XML should look like this: Debug.Print "<item xmlns=""http://abc...xyz""><Test xmlns="""">Test Node Text</Test><name supplier=""Contoso"">Hammer</name><price>11</price></item>" 'With the new node "Test" inserted before the defined node "name" 'What am I missing?End Sub
可是结果是错误的。原因有2:
1. 所先的结点不对,上面代码所先的节点是要插入节点的兄弟节点。实际上应该选这个节点的父节点。
2. 方法不对InsertNodeBefore会将要插入的内容插入到所选节点的未尾。http://msdn.microsoft.com/en-us/library/office/microsoft.office.core.customxmlnode.insertnodebefore(v=office.14).aspx
解决方案是用InsertSubtreeBefore代码如下:
Sub HowDoesInsertNodeBeforeWork()Dim oCXPart As CustomXMLPartDim oCXNode As CustomXMLNodeDim strXML As StringstrXML = "<?xml version='1.0' ?><invoice xmlns='http://abc...xyz'>" _ & "<items>" _ & "<item ><name supplier='Home Depot'>Hammer</name><price>12</price></item>" _ & "<item ><name supplier='Lowes'>Hammer</name><price>12</price></item>" _ & "<item ><name supplier='Contoso'>Hammer</name><price>11</price></item>" _ & "</items>" _ & "</invoice>" On Error Resume Next ActiveDocument.CustomXMLParts(4).Delete On Error GoTo 0 'Add the part. Set oCXPart = ActiveDocument.CustomXMLParts.Add 'Load the XML oCXPart.LoadXML strXML 'Define a node. Set oCXNode = oCXPart.SelectSingleNode("/ns0:invoice[1]/ns0:items[1]/ns0:item[3][ns0:name[@supplier='Contoso']]") Set xt = oCXPart.SelectSingleNode("/ns0:invoice[1]/ns0:items[1]/ns0:item[3]/ns0:name[@supplier='Contoso']") Debug.Print oCXNode.BaseName Debug.Print oCXNode.XPath 'Attempt to insert a node before the defined node. 'oCXNode.InsertNodeBefore "Test", , msoCustomXMLNodeElement, "Test Node Text" oCXNode.InsertSubtreeBefore "<test>Test Node Text</test>", xt Debug.Print oCXPart.SelectSingleNode("/ns0:invoice[1]/ns0:items[1]/ns0:item[3][ns0:name[@supplier='Contoso']]").XML 'Seems the XML should look like this: Debug.Print "<item xmlns=""http://abc...xyz""><Test xmlns="""">Test Node Text</Test><name supplier=""Contoso"">Hammer</name><price>11</price></item>" 'With the new node "Test" inserted before the defined node "name" 'What am I missing?End Sub
- VBA Word CustomXMLPart 中在指定的Node"前添加Node
- 如何在VPP中添加graph node
- VBA技术在WORD中添加自定义菜单调用宏
- 在node-webkit中使用node modules
- VBA: EXCEL中插入指定的行数并添加值
- 查找neo4j数据库中没有指定node类型的节点
- 在word中使用vba遍历Range对象的Characters
- 如何在node节点的评论下面添加想要…
- 如何在node节点的评论下面添加想要…
- docker swarm如何在指定的node上运行指定的容器
- 在node中使用mongodb
- 在node中使用 ES6
- 关于在Drupal中使用REST Server 增加node时有taxonomy_term 和 node reference的解决方法
- sublime text3 配置node环境在path中找不到node的解决
- 通过brew安装指定版本的node
- kubernetes Pod 调度到指定的 Node
- Node.js--Hello word!
- 【Node】node的模块
- 通过hidden隐藏域和URL参数(在一个处理页面实现不同的处理内容)
- Oracle中的dual表
- Smarty操作数据库
- 让Ubuntu装上自己编译的kernel
- Notepad++ 是什么软件
- VBA Word CustomXMLPart 中在指定的Node"前添加Node
- Linux SSH远程文件/目录传输命令scp
- ATL
- 框架
- 修改VMware虚拟机网卡MAC地址的方法总结
- zencart 标题 关键词 描述 SEO优化
- MongoDB
- mysql增加普通用户后无法登陆问题的解决方法
- objdump(GCC)命令的用法