Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2024, 07:36 AM
Omar2008 Omar2008 is offline Convert XML to Word(Content Control) Windows 10 Convert XML to Word(Content Control) Office 2013
Novice
Convert XML to Word(Content Control)
 
Join Date: Feb 2024
Location: Lima Perú
Posts: 4
Omar2008 is on a distinguished road
Default Convert XML to Word(Content Control)

Good morning:
I am trying to convert XML file into Content Controls (Word)
Each record in different pages

XML file D:\Ex03Rev1.xml (zip file)
The prototype word file with the content controls is:
D:\Ex05.docx which which has 3 current custom XML parts
attachment: ("Ex05 current customs XML parts.png")

Win 2010 & Office 2013 with Ms XML 6.0
attachment: ("Ex05macro xml 6.png")

The code is in Ex05macro.docx
The code can not add xml part to the word file, then the following is not possible (link each record to content control
By other side, my understanding of XML 6.0 is limited. I don't know where, in the code, the number 4 corresponding to the new custom xml should go.

Please your help
thank
Omar

Sub Ex05macro()
' Dim xmlDoc As MSXML2.DOMDocument60
' Set xmlDoc = New MSXML2.DOMDocument60
' xmlDoc.Load ("d:\Ex03Rev1.xml")
' xmlDoc.async = False

' Dim docXML As IXMLDOMNode ' Define miNodo como IXMLDOMNode para la iteración
'Set docXML = New MSXML2.DOMDocument60

'Set docXLM = GetXMLDocument()

Dim myDoc As Document
Set myDoc = Documents.Open("d:\Ex05.docx")


myDoc.CustomXMLParts.Add
myDoc.CustomXMLParts(4).Load ("d:\Ex03Rev1.xml")
Debug.Print myDoc.CustomXMLParts(4).Errors
' For Each miNodo In xlmDoc.SelectNodes("//RECORD")

' myDoc.Selection.InsertBreak Type:=wdPageBreak
Dim strXPath As String
strXPath = "/RECORD/ID"
myDoc.ContentControls(1).XMLMapping.SetMapping strXPath
strXPath = "/RECORD/ONR"
myDoc.ContentControls(2).XMLMapping.SetMapping strXPath
strXPath = "/RECORD/TAREA"
myDoc.ContentControls(3).XMLMapping.SetMapping strXPath
strXPath = "/RECORD/RECURSOS"
myDoc.ContentControls(4).XMLMapping.SetMapping strXPath
strXPath = "/RECORD/PRIORIDAD"
myDoc.ContentControls(5).XMLMapping.SetMapping strXPath
strXPath = "/RECORD/AERODROMO"
myDoc.ContentControls(6).XMLMapping.SetMapping strXPath
strXPath = "/RECORD/ESTATUS"
myDoc.ContentControls(7).XMLMapping.SetMapping strXPath
' Next miNodo
myDoc.SaveAs "d:\Ex05ONR.docx"
myDoc.Close
' Set xmlDoc = Nothing
End Sub
Attached Images
File Type: png Ex05 current customs XML parts.png (49.1 KB, 9 views)
File Type: png Ex05macro xml 6.png (16.1 KB, 9 views)
Attached Files
File Type: docx Ex05.docx (20.3 KB, 1 views)
File Type: docm Ex05macro.docm (17.7 KB, 0 views)
File Type: zip Ex03Rev1.zip (741 Bytes, 1 views)
Reply With Quote
  #2  
Old 02-09-2024, 08:22 AM
Italophile Italophile is offline Convert XML to Word(Content Control) Windows 11 Convert XML to Word(Content Control) Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Just take a step back and think for a moment. You have a file containing eight records. When you attempt to map a control to an XPath of "/RECORD/ID", which of the eight identical XPaths should it map to?
Reply With Quote
  #3  
Old 02-09-2024, 09:40 AM
Omar2008 Omar2008 is offline Convert XML to Word(Content Control) Windows 10 Convert XML to Word(Content Control) Office 2013
Novice
Convert XML to Word(Content Control)
 
Join Date: Feb 2024
Location: Lima Perú
Posts: 4
Omar2008 is on a distinguished road
Default

Italo thank you for your interest
My goal is to convert 8 XML records in 8 word pages (one per each objective)
I am trying with content controls and I believe that it is possible to use command FOR EACH to link each xml element into a different page

For each miNodo In xmlDoc.SelectNodes("//RECORD")

But if there is another way I would be great
Again thanks for your help.
Reply With Quote
  #4  
Old 02-09-2024, 10:23 AM
Italophile Italophile is offline Convert XML to Word(Content Control) Windows 11 Convert XML to Word(Content Control) Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

I can't remember what support Word 2013 has for custom xml parts.

In Word 365 from the XML Mapping pane I can:
  1. load your xml file,
  2. map the fields of the first record to content controls
  3. add a manual page break after the last field
  4. select all the above
  5. then map the records to a repeating section content control
This results in all records being added to the document, with each one on its own page.

One point to note is that your xml file does not have a namespace. Whilst not critical it is useful to have the namespace to identify the xml part.
Reply With Quote
  #5  
Old 02-09-2024, 10:54 AM
Italophile Italophile is offline Convert XML to Word(Content Control) Windows 11 Convert XML to Word(Content Control) Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
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
  #6  
Old 02-09-2024, 02:34 PM
Omar2008 Omar2008 is offline Convert XML to Word(Content Control) Windows 10 Convert XML to Word(Content Control) Office 2013
Novice
Convert XML to Word(Content Control)
 
Join Date: Feb 2024
Location: Lima Perú
Posts: 4
Omar2008 is on a distinguished road
Default

Italo
Thank you so much

It works. Awesome
Reply With Quote
  #7  
Old 02-09-2024, 02:51 PM
Omar2008 Omar2008 is offline Convert XML to Word(Content Control) Windows 10 Convert XML to Word(Content Control) Office 2013
Novice
Convert XML to Word(Content Control)
 
Join Date: Feb 2024
Location: Lima Perú
Posts: 4
Omar2008 is on a distinguished road
Default

Italo
Thank
With your help now it is possible to create large reports automatically and online.

Notice that the XML file can be generated by access, excel or other app where you have the actual data. If you have the xls or access data in collaborative place a lot of people can populate the data online

Thanks so much
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adjust content antd line spacing in word table according to checkbox content control state AVarg123 Word VBA 13 01-10-2024 03:36 PM
Convert XML to Word(Content Control) Change Value of a Content Control based on the value of another content Control jsc_msoffice Word VBA 2 05-21-2021 09:51 PM
Convert XML to Word(Content Control) Content Control Dropdowns - How to duplicate the content control and allow a second choice ashleyf Word VBA 2 03-19-2020 09:11 AM
Convert XML to Word(Content Control) Clicking the selected Content Control checkbox returns wrong control in vba event DougsGraphics Word VBA 2 06-24-2015 07:31 AM
Convert XML to Word(Content Control) Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft