none
Iterating XML nodes using VBA - Word 2007 VBA: Retreiving and iterating ActiveDocument.CustomXMLParts RRS feed

  • Question

  • 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?





      

    • Moved by Bessie Zhao Friday, December 31, 2010 9:13 AM (From:General Office Development)
    Thursday, December 30, 2010 6:37 PM

Answers

  • Ok, this is how I did it. Posting just in case it is useful for somebody. Apparently, you need not use "CustomXMLNodes" object. This can still be done using SelectNodes. The below function shows that. Also, I was not checking for empty string while adding item to the list.

    Sub LoadItems()
         
    Dim totalItemsCount As Integer
         totalItemsCount
    = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes.Count
         
    Dim item As String

         
    For i = 1 To totalItemsCount
            item
    = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes(i).text
            item
    = Replace(item, " ", Empty)

           
    If Len(item) > 1 Then
           
    ItemUserControl.lstItems.AddItem pvargItem:item
           
    End If
         
    Next i
    End Sub

    • Marked as answer by ashishmgupta Tuesday, January 4, 2011 10:18 AM
    Tuesday, January 4, 2011 9:16 AM

All replies

  • Hello ashishmgupta,

    Thanks for posting. Since this issue is more a Word development issue, I have moved this thread from General Office development forum to Word for Developers forum.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience. Thank you for your understanding and support. Have a nice weekend.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 31, 2010 9:16 AM
  • Hi Ashish

    Try:

    Dim itemNode as Office.CustomXMLNode
    Dim itemNodes as Office.CustomXMLNodes

    This is a bit confusing, I know. In Word 2003 through 2007 it was possible to "drop" XML Nodes from an attached schema onto the document surface. These are XMLNodes (It's still possible in 2010, but they're removed the next time the document is opened. The feature was deprecated for Office 2010 due to a patent hassle in the US courts.)

    Custom XML Parts are available for all types of Office document (Excel workbook, PowerPoint presentation), so this part of the object model is in the Office "namespace" (type library).


    Cindy Meister, VSTO/Word MVP
    Friday, December 31, 2010 9:40 AM
    Moderator
  • Cindy,

    Apparently, 

    ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(0).ChildNodes

    should have been

    ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes

    and now I am able to iterate through the child nodes using the following code and now trying to add each item in a list box which I have placed in user form:-
     Dim itemsCount As Integer
    Set itemsCount = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes.Count


    For i = 1 To itemsCount 
     styleName = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//TableStyles")(1).ChildNodes(i).text
     UserForm1.lstBox.AddItem ( styleName, cstr(i))
     Next i


    In the above code in the following line 
     UserForm1.lstBox.AddItem ( styleName, cstr(i))
    I am getting the following error :-
    Invalid argument -2147024809

    I am able to see the values for stylename and i in the quickwatch window. Could you please point out where I am going wrong?
    Friday, December 31, 2010 10:15 AM
  • I think you probably need

     UserForm1.lstBox.AddItem ( styleName, cstr(i-1))

    or maybe just

     UserForm1.lstBox.AddItem styleName



    Peter Jamieson
    Friday, December 31, 2010 3:25 PM
  • Try to use the following, without quotes.

    "UserForm1.lstBox.AddItem styleName, i"

    Index Optional Variant The position of the item in the list. If this argument is omitted, the item is added to the end of the list.
    The second argument is optional for AddItem, but it should be the position of the item in the list so using an integer should work.  I was able to get that working on my machine.

    Brandon

    Friday, December 31, 2010 5:04 PM
    Moderator
  • Ok, this is how I did it. Posting just in case it is useful for somebody. Apparently, you need not use "CustomXMLNodes" object. This can still be done using SelectNodes. The below function shows that. Also, I was not checking for empty string while adding item to the list.

    Sub LoadItems()
         
    Dim totalItemsCount As Integer
         totalItemsCount
    = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes.Count
         
    Dim item As String

         
    For i = 1 To totalItemsCount
            item
    = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes(i).text
            item
    = Replace(item, " ", Empty)

           
    If Len(item) > 1 Then
           
    ItemUserControl.lstItems.AddItem pvargItem:item
           
    End If
         
    Next i
    End Sub

    • Marked as answer by ashishmgupta Tuesday, January 4, 2011 10:18 AM
    Tuesday, January 4, 2011 9:16 AM
  • HI ashim

    Thanks for posting your solution. Glad you have it working to your satisfaction :-)

    You're able to do this without using CustomXMLNodes because you're accessing the text node of the element directly. Of course that's allowed if no "xmlNode" object is required (which was the case with your original question).


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 4, 2011 9:44 AM
    Moderator
  • Thank you Cindy. :-) It was a good discussion.
    Tuesday, January 4, 2011 10:19 AM