VBA Word CustomXMLPart 中在指定的Node"前添加Node

32 篇文章 0 订阅
23 篇文章 1 订阅

网上我看见有人想用InsertNodeBefore来实现这个想法。代码如下:

Sub HowDoesInsertNodeBeforeWork()
Dim oCXPart As CustomXMLPart
Dim oCXNode As CustomXMLNode
Dim strXML As String
strXML = "<?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 CustomXMLPart
Dim oCXNode As CustomXMLNode
Dim strXML As String
strXML = "<?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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Word VBA向新建选项卡组添加子按钮,可以按照以下步骤进行操作: 1. 打开Word文档,并按下Alt+F11打开VBA编辑器; 2. 在VBA编辑器,选择“插入”->“用户窗体”->“Ribbon XML”; 3. 在Ribbon XML编辑器,编写XML代码来创建新的选项卡组和子按钮,例如: ``` <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"> <ribbon> <tabs> <tab id="customTab" label="My Custom Tab"> <group id="customGroup" label="My Custom Group"> <button id="customButton" label="My Custom Button" onAction="MyCustomButton_Click" size="large" /> <menu id="customMenu" label="My Custom Menu"> <button id="customSubMenu1" label="Submenu Item 1" onAction="MyCustomSubMenu1_Click" /> <button id="customSubMenu2" label="Submenu Item 2" onAction="MyCustomSubMenu2_Click" /> </menu> </group> </tab> </tabs> </ribbon> </customUI> ``` 4. 在VBA项目添加一个新的模块并编写按钮的点击事件处理程序,例如: ``` Sub MyCustomButton_Click(control As IRibbonControl) ' Do something when the button is clicked End Sub Sub MyCustomSubMenu1_Click(control As IRibbonControl) ' Do something when submenu item 1 is clicked End Sub Sub MyCustomSubMenu2_Click(control As IRibbonControl) ' Do something when submenu item 2 is clicked End Sub ``` 5. 在VBA项目添加一个新的类模块,并将其命名为“MyRibbon”,以便在XML代码引用它; 6. 在MyRibbon类模块添加一个回调函数以返回Ribbon XML代码,例如: ``` Public Function GetCustomUI(ByVal RibbonID As String) As String GetCustomUI = "<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='MyImageLoader'>" & _ " <ribbon>" & _ " <tabs>" & _ " <tab id='customTab' label='My Custom Tab'>" & _ " <group id='customGroup' label='My Custom Group'>" & _ " <button id='customButton' label='My Custom Button'" & _ " onAction='MyCustomButton_Click' size='large' />" & _ " <menu id='customMenu' label='My Custom Menu'>" & _ " <button id='customSubMenu1' label='Submenu Item 1'" & _ " onAction='MyCustomSubMenu1_Click' />" & _ " <button id='customSubMenu2' label='Submenu Item 2'" & _ " onAction='MyCustomSubMenu2_Click' />" & _ " </menu>" & _ " </group>" & _ " </tab>" & _ " </tabs>" & _ " </ribbon>" & _ "</customUI>" End Function ``` 7. 在VBA项目添加另一个模块并编写代码来加载Ribbon XML,例如: ``` Public Sub LoadCustomUI() Dim ribbon As IRibbonUI Set ribbon = Application.CommandBars("Ribbon"). _ Controls.Add(msoControlCustom, , "MyRibbon") ribbon.Invalidate End Sub ``` 8. 最后,在Word文档添加一个宏以调用LoadCustomUI函数,例如: ``` Sub InitializeCustomUI() LoadCustomUI End Sub ``` 这样就可以在新建选项卡组添加子按钮了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值