#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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?
|
#3
|
|||
|
|||
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. |
#4
|
|||
|
|||
I can't remember what support Word 2013 has for custom xml parts.
In Word 365 from the XML Mapping pane I can:
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. |
#5
|
|||
|
|||
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 |
#6
|
|||
|
|||
Italo
Thank you so much It works. Awesome |
#7
|
|||
|
|||
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 |
|
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 |
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 |
Content Control Dropdowns - How to duplicate the content control and allow a second choice | ashleyf | Word VBA | 2 | 03-19-2020 09:11 AM |
Clicking the selected Content Control checkbox returns wrong control in vba event | DougsGraphics | Word VBA | 2 | 06-24-2015 07:31 AM |
Deleting a table from a content control -- preserving the content control | BrainSlugs83 | Word Tables | 8 | 11-14-2013 03:06 AM |