在Word文档中迭代CustomXmlDocument

以下代码是一个VBA Form中的,用来迭代CustomXmlDocument。

Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)

End Sub

Private Sub UserForm_Activate()
    Dim oCustXMLPart As CustomXMLPart
    Dim oNode As CustomXMLNode
    Dim oNodeChild As CustomXMLNode
    Dim oNodeChildChild As CustomXMLNode
    Dim i As Long
    Dim tRoot As Node
    
    On Error Resume Next
    ActiveDocument.CustomXMLParts(4).Delete
    On Error GoTo 0
    Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add
    oCustXMLPart.LoadXML ("<DocElement><Name>Joe</Name><Age>41</Age><Multi><Item1>A</Item1><Item2>B</Item2></Multi></DocElement>")
    Set oNode = oCustXMLPart.DocumentElement
    Set tRoot = TreeView1.Nodes.Add
    tRoot.Text = "DocElement"
    tRoot.Key = "DocElement"
    Iterator oNode, tRoot
End Sub

Sub Iterator(ByRef Element As CustomXMLNode, ByRef tNode As Node)
    Dim Childe As CustomXMLNode
    Dim subNode As Node
    For Each Childe In Element.ChildNodes
        If Childe.NodeType = msoCustomXMLNodeText Then
            Set subNode = TreeView1.Nodes.Add(tNode.Key, tvwChild, Childe.XPath, Childe.Text)
        ElseIf Childe.NodeType = msoCustomXMLNodeElement Then
            Set subNode = TreeView1.Nodes.Add(tNode.Key, tvwChild, Childe.XPath, Childe.BaseName)
            If Childe.HasChildNodes Then
                Iterator Childe, subNode
            End If
        End If
    Next
End Sub


你可能感兴趣的:(document)