ReplaceInTemplate.vb
C# VB
'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.IOImports System.LinqImports GrapeCity.Documents.Word
'' This sample shows how to fill a template document with data'' using the RangeBase.Replace extension method provided by '' the RangeBaseFindReplaceExtensions class.'''' The template document Is similar to the document generated by'' the ProcurementLetter sample, but the data values in that document'' were changed to template placeholders (in the form '__%<name>%__')'' using MS Word. In the sample, we use the text replace functionality'' to change the placeholders back to original data. The resulting'' document can be viewed by running the ProcurementLetterTpl sample.'''' In a real life application, the same approach can be used for example'' to go over a record set, generating a document for each record.Public Class ReplaceInTemplate Public Function CreateDocx() As GcWordDocument
'' Placeholders And sample data: Dim placeholderData As (placeholder As String, data As String)() = { ("__%c_name%__", "Nancy Davolio"), ("__%c_title%__", "Chief Procurement Officer"), ("__%c_phone%__", "555-543-5432"), ("__%c_url%__", "www.acmeinc.com"), ("__%c_address%__", "5432 Street West, Townsvilla, State 54321"), ("__%to_name%__", "Mark"), ("__%dlr_name%__", "AMA Ltd"), ("__%order_no%__", "8393") }
'' Load the document: Dim doc = New GcWordDocument() doc.Load(Path.Combine("Resources", "WordDocs", "ProcurementLetterTpl.docx"))
'' Replace all placeholders in the body with sample data values For Each pair In placeholderData doc.Body.Replace(pair.placeholder, pair.data) Next
'' Done Return doc End FunctionEnd Class