View Single Post
 
Old 09-03-2020, 09:26 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,990
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

My approach to this is different to Greg's. For my method, I've chosen to add an XMLMap to your Excel workbook and apply it to your tabular datasource. Then use a macro in Word to open the Excel workbook and read that XML data into a string which is then loaded as a CustomXML file stored in the Word document itself.

Once that is done, you can use the Developer Tab's XML Mapping Pane to insert linked Plain Text Content Controls to your Word document anywhere you want this data to appear. The Word document can be saved in that state and if the Excel source is updated, rerun the macro to refresh the CustomXML in the Word document. Any updated CCs in the document will automatically be refreshed as soon as the CustomXML is updated.

The macro to use in Word is:
Code:
Sub GetNewXML()
  Dim appXL As Object, oWB As Object, oMap As Object, sPath As String, sXML As String
  Dim oXPart As CustomXMLPart, sNS As String
  
  sNS = "SomeNameSpace"
  sPath = ActiveDocument.Path & Application.PathSeparator & "customer's_dummy_data.xlsx"
  Set appXL = CreateObject("Excel.Application")
  Set oWB = appXL.Workbooks.Open(sPath)
  Set oMap = oWB.XmlMaps("root_Map")
  oMap.ExportXML Data:=sXML
  sXML = Replace(sXML, "<root>", "<root xmlns='" & sNS & "'>")
  Debug.Print sXML
  oWB.Close False
  appXL.Quit
  Set oMap = Nothing
  Set oWB = Nothing
  Set appXL = Nothing
  
  'If CustXML with same Namespace already there, remove it
  For Each oXPart In ActiveDocument.CustomXMLParts
    If oXPart.NamespaceURI = sNS Then
      oXPart.Delete
    End If
  Next
  
  'Now add new CustomXML
  Set oXPart = ActiveDocument.CustomXMLParts.Add(sXML)
  
End Sub
The source Excel file which contains your data and XML Map is attached.

If you can get this working for a specific number of records, we can look at ways to automate the insertion or removal of content when the total number of records varies. I can't suggest ways to do that from your document yet because I don't understand the language and can't identify the repeating components.
Attached Files
File Type: xlsx customer's_dummy_data.xlsx (24.9 KB, 20 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote