I realize this is a rather arcane subject. Still hoping someone can help directly or direct me to a better forum.
I have a project where I create a basic document CustomXMLPart and then need to regularly add (append) childnodes to the document element. The following code pasted into a standard module will illustrate the basic process.
The .AppendChildNode method doesn't seem to behave correctly. The MS documentation states that is returns a CustomXMLNode, but direct attempts to do so results in a compile error.
Does anyone see what I might be doing wrong or have a suggestion for a better way? Thanks.
Private m_oXMLPart As CustomXMLPart
Sub BuildDefaultAddressBook()
Dim strXML As String
On Error Resume Next
ThisDocument.CustomXMLParts.SelectByNamespace("http://somenamespace/GKM").Item(1).Delete
On Error GoTo 0
strXML = "<?xml version='1.0'?>" & vbLf _
& "<DocumentElement_Test xmlns='http://somenamespace/GKM'>" & vbLf _
& "</DocumentElement_Test>"
On Error Resume Next
Set m_oXMLPart = ThisDocument.CustomXMLParts.SelectByNamespace("http://somenamespace/GKM").Item(1)
On Error GoTo 0
If m_oXMLPart Is Nothing Then
ThisDocument.CustomXMLParts.Add strXML
Set m_oXMLPart = ThisDocument.CustomXMLParts.SelectByNamespace("http://somenamespace/GKM").Item(1)
End If
AddChildNodesInDocumentElement
lbl_Exit:
Exit Sub
End Sub
Sub AddChildNodesInDocumentElement()
Dim oEntries As CustomXMLNodes, oEntry As CustomXMLNode
Dim oNewEntry As CustomXMLNode
'Create (add/append) a childnode in the document element
'According to the MS documentation, AppendChildNode returns a CustomXMLNode
'However, the following line (stetted out) will not compile, "Compile Error - Expected function or variable"
'Set oNewEntry = m_oXMLPart.DocumentElement.AppendChildNode("Entry", , msoCustomXMLNodeElement)
'So , I 'm doing it like this:
m_oXMLPart.DocumentElement.AppendChildNode "Entry", , msoCustomXMLNodeElement
'Get the colletion of childnodes.
Set oEntries = m_oXMLPart.SelectNodes("ns0:DocumentElement_Test[1]/*")
'Get the entry just created which is the last one
Set oNewEntry = oEntries.Item(oEntries.Count)
'Now I have my new entry node.
With oNewEntry
.AppendChildNode "FirstName", , msoCustomXMLNodeElement, "Greg"
.AppendChildNode "LastName", , msoCustomXMLNodeElement, "Maxey"
End With
lbl_Exit:
Exit Sub
End Sub
Cross posted in Office - Microsoft Answers Programming
Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm