Using Excel VBA to create an XML file

excel, vba, xml

Solution

HEADING1 is the text that belongs to the `TextField0` element, not the attribute.

So you can set it using the `objXmlElement.text` property.

In response to comment:

I'm struggling to find documentation for this stuff

This MSDN page is a good start.

Problem

I'm trying to create an XML file from VBA found in Excel, I'm a little hazy on the details though, I've never touched XML or VBA before in my life. I need to write out an XML file with in the following format: ``` <TextField0 xfdf:original="Brand Name">HEADING1</TextField0> ``` There will be a bunch of lines like this such as TextField1, which is "Product" and so on. The code I have thus far revelent to this section is this: ``` 'create Heading element Set objXMLelement = objDom.createElement("TextField0") objXMLRootelement.appendChild objXMLelement 'create Attribute to the Heading Element and set value Set objXMLattr = objDom.createAttribute("xfdf:original") objXMLattr.NodeValue = "Brand Name" objXMLelement.setAttributeNode objXMLattr ``` That creates this output: ``` <TextField0 xfdf:original="Brand Name"/> ``` This is of coursem missing HEADING1 and I can't for the life of me figure out how to put that bit in there. I can't seem to append anything to an attribute. Any help will be gratefully received.

Original source