Iterating XML nodes using VBA

Posted by ydobonmai on Stack Overflow See other posts from Stack Overflow or by ydobonmai
Published on 2010-12-30T18:28:31Z Indexed on 2011/01/03 0:53 UTC
Read the original article Hit count: 286

Filed under:
|
|

Note:- It just might be a iterating XML nodes using VBA question. Please look at the bottom of this question. It would be good If we can iterate without using MSXML2.DOMDocument

I see the this question which answers part of my question on how to retrieve the CustomXMLPart. However, I am not able to iterate through the Xml. That way, this might not be specific to CustomXmlPart, It just might be a iterating XML using VBA question. Following is the XML I have in my CustomXMLPart.

<Items>
<Item1>Item1</Item1>
<Item2>Item2</Item2>
<Item3>Item3</Item3>
</Items>

This is how I add the above XML as CustomXmlPart:-

static void AddCustomTableXmlPart(WordprocessingDocument document)
        {
            MainDocumentPart mainDocumentPart = document.MainDocumentPart;
            XDocument itemXml = GetItemsAsCustomXML();

            if (mainDocumentPart.GetPartsCountOfType<CustomXmlPart>() > 0)
                mainDocumentPart.DeleteParts<CustomXmlPart>(mainDocumentPart.CustomXmlParts);

            //Add a new customXML part and then add content
            var customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            //copy the XML into the new part...
            using (var ts = new StreamWriter(customXmlPart.GetStream()))
            {
                ts.Write(itemXml.ToString());
                ts.Flush();
            }
        }

and this is how I am accessing it in the macro:-

Dim itemNode As xmlNode
Dim itemChildren As XMLNodes

' The below line throws a run-time error 'Run-time error '13' - 'type mismatch ' not sure why.

**Set itemChildren= ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectSingleNode("//Items").ChildNodes**

Interestingly, when I quick watch ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectSingleNode("//Items").ChildNodes, I see child items in the quick watch window. Is the assignment to the itemChildren variable incorrect?

I want to iterate through all the items and get get text for all of them. Could anybody help?

© Stack Overflow or respective owner

Related posts about Xml

Related posts about vba