View Single Post
 
Old 02-09-2024, 10:54 AM
Italophile Italophile is offline Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 554
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

I have not previously worked with custom xml parts with multiple records, so this has been a fun challenge.

The following code should get you most of the way there, though I can't guarantee that it will work in Office 2013:

Code:
Sub Ex05macro()
    Dim myDoc As Document: Set myDoc = Documents.Add
    Dim myXPart As CustomXMLPart: Set myXPart = myDoc.CustomXMLParts.Add
    Dim xNode As CustomXMLNode, xChild As CustomXMLNode
    Dim cc As ContentControl
    With myXPart
        .Load ("D:\Ex03Rev1.xml")
        For Each xNode In .DocumentElement.ChildNodes
            If xNode.HasChildNodes Then
                For Each xChild In xNode.ChildNodes
                    With xChild
                        If Not .BaseName = "#text" Then
                            Set cc = myDoc.ContentControls.Add(Type:=wdContentControlText, Range:=myDoc.Characters.Last)
                            cc.XMLMapping.SetMapping .XPath
                            myDoc.Paragraphs.Add
                        End If
                    End With
                Next xChild
                myDoc.Characters.Last.InsertBreak WdBreakType.wdPageBreak
            End If
        Next xNode
    End With
End Sub
Reply With Quote