以下代码是一个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

本文介绍了一个使用VBAForm在Word文档中操作CustomXmlDocument的例子,通过编写代码实现对XML元素的遍历和展示,包括添加、删除节点以及展示XML文档的层次结构。
1405

被折叠的 条评论
为什么被折叠?



