'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.LinqImportsSystem.GlobalizationImportsGrapeCity.Documents.Word '' This sample demonstrates the use of DataTemplate.BatchProcess() method,'' which allows you to loop over the root items of a data source, generating'' a separate DOCX for each data source item.PublicClassDataTplBatchOceansFunctionCreateDocx() AsGcWordDocument'' The data source (ocean And sea data from Wikipedia):Dim oceans = {NewWith {.name = "Pacific", .areaOfWorldOcean = 0.466, .volumeOfWorldOcean = 0.501, .seas = {NewWith {.name = "Australasian Mediterranean Sea"}, NewWith {.name = "Philippine Sea"}, NewWith {.name = "Coral Sea"}, NewWith {.name = "South China Sea"}}},NewWith {.name = "Atlantic", .areaOfWorldOcean = 0.235, .volumeOfWorldOcean = 0.233, .seas = {NewWith {.name = "Sargasso Sea"}, NewWith {.name = "Caribbean Sea"}, NewWith {.name = "Mediterranean Sea"}, NewWith {.name = "Gulf of Guinea"}}},NewWith {.name = "Indian", .areaOfWorldOcean = 0.195, .volumeOfWorldOcean = 0.198, .seas = {NewWith {.name = "Arabian Sea"}, NewWith {.name = "Bay of Bengal"}, NewWith {.name = "Andaman Sea"}, NewWith {.name = "Laccadive Sea"}}},NewWith {.name = "Southern", .areaOfWorldOcean = 0.061, .volumeOfWorldOcean = 0.054, .seas = {NewWith {.name = "Weddell Sea"}, NewWith {.name = "Somov Sea"}, NewWith {.name = "Riiser-Larsen Sea"}, NewWith {.name = "Lazarev Sea"}}},NewWith {.name = "Arctic", .areaOfWorldOcean = 0.043, .volumeOfWorldOcean = 0.014, .seas = {NewWith {.name = "Barents Sea"}, NewWith {.name = "Greenland Sea"}, NewWith {.name = "East Siberian Sea"}, NewWith {.name = "Kara Sea"}}} } '' Create And load the template DOCX:Dim doc = NewGcWordDocument() doc.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx")) '' Add the data source to the data template data sources doc.DataTemplate.DataSources.Add("ds", oceans) '' In this sample, we use the DataTemplate.BatchProcess() method which iterates over the root data items,'' generating a separate document for each item. To allow the sample to show the result as a single document,'' we add each generated document as a separate section to a New DOCX.'' Note that when DataTemplate.BatchProcess() finishes processing, the original GcWordDocument on which'' it was called reverts to its initial state (i.e. it contains the unprocessed template tags rather than data).'' If you want to preserve the results of the batch processing, you must save the document in its current'' state each time the 'itemProcessed' action gets called. '' The document to contain all results:Dim allDocs = NewGcWordDocument()'' This makes sure the resulting document has the same styles as the template: allDocs.Load(Path.Combine("Resources", "WordDocs", "BatchOceansTemplate.docx")) allDocs.Body.Clear() '' The extra section break:Dim lastSection AsSection = Nothing '' The callback method called as each root data item Is processed:Dim itemProcessed As Action =Sub()'' In order to keep the processed template, we must either save the original document'' in its current state to a file, Or copy its content to another document as we do here,'' as the content Is regenerated for each root data item, And Is reset to the original'' template when processing completes: doc.Body.CopyTo(allDocs.Body, InsertLocation.End, FormattingCopyStrategy.Copy) lastSection = allDocs.Body.Paragraphs.Last.AddSectionBreak()EndSub '' Batch process the template: doc.DataTemplate.BatchProcess(itemProcessed, CultureInfo.GetCultureInfo("en-US"))'' Delete the extra section break: lastSection.Delete() '' DoneReturn allDocsEndFunctionEndClass