0

I'm editing an XML document with VBA code. I want to build up a new, nested set of elements and then use replaceChild to substitute this portion of XML for a similar, existing element. I am getting run-time error 438 when trying to insert a text node into my first new element.

I copied this approach from a tutorial example I found online so expected it to work without a hitch.

I'm trying to build up this section of XML:

<ConnectionProperties>
<DataProvider>SQL</DataProvider> 
</ConnectionProperties>

To build up the elements:

Dim NewElement As MSXML2.IXMLDOMElement
Dim NewNodeElement As MSXML2.IXMLDOMElement
Dim NewNodeText As MSXML2.IXMLDOMText

Set NewElement = xmlDoc.createNode(1, "ConnectionProperties", xmlNameSpace)

Set NewNodeElement = xmlDoc.createNode(1, "DataProvider", xmlNameSpace)
Set NewNodeText = xmlDoc.createTextNode("SQL")

NewNodeElement.appendChild (NewNodeText)    <--- Get run-time error 438 here

NewElement.appendChild (NewNodeElement)

I get the run-time error attempting to insert the text value "SQL" into the DataProvider tag.

I expected the text to be inserted into the element but I get a run-time error instead.

Erik A
  • 31,639
  • 12
  • 42
  • 67
Derek Jones
  • 47
  • 1
  • 1
  • 4
  • 4
    `NewNodeElement.appendChild NewNodeText` with no parentheses. Adding parentheses causes an item to be *evaluated* - the result of that may be different from the enclosed item. – Tim Williams May 16 '19 at 17:30
  • See related: https://stackoverflow.com/questions/22186853/unexpected-results-from-typename/22188933#22188933 – Tim Williams May 16 '19 at 19:33

0 Answers0