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

網上我看見有人想用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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章