ReplaceInTemplate.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Linq
  8. Imports GrapeCity.Documents.Word
  9.  
  10. '' This sample shows how to fill a template document with data
  11. '' using the RangeBase.Replace extension method provided by
  12. '' the RangeBaseFindReplaceExtensions class.
  13. ''
  14. '' The template document Is similar to the document generated by
  15. '' the ProcurementLetter sample, but the data values in that document
  16. '' were changed to template placeholders (in the form '__%<name>%__')
  17. '' using MS Word. In the sample, we use the text replace functionality
  18. '' to change the placeholders back to original data. The resulting
  19. '' document can be viewed by running the ProcurementLetterTpl sample.
  20. ''
  21. '' In a real life application, the same approach can be used for example
  22. '' to go over a record set, generating a document for each record.
  23. Public Class ReplaceInTemplate
  24. Public Function CreateDocx() As GcWordDocument
  25.  
  26. '' Placeholders And sample data:
  27. Dim placeholderData As (placeholder As String, data As String)() =
  28. {
  29. ("__%c_name%__", "Nancy Davolio"),
  30. ("__%c_title%__", "Chief Procurement Officer"),
  31. ("__%c_phone%__", "555-543-5432"),
  32. ("__%c_url%__", "www.acmeinc.com"),
  33. ("__%c_address%__", "5432 Street West, Townsvilla, State 54321"),
  34. ("__%to_name%__", "Mark"),
  35. ("__%dlr_name%__", "AMA Ltd"),
  36. ("__%order_no%__", "8393")
  37. }
  38.  
  39. '' Load the document:
  40. Dim doc = New GcWordDocument()
  41. doc.Load(Path.Combine("Resources", "WordDocs", "ProcurementLetterTpl.docx"))
  42.  
  43. '' Replace all placeholders in the body with sample data values
  44. For Each pair In placeholderData
  45. doc.Body.Replace(pair.placeholder, pair.data)
  46. Next
  47.  
  48. '' Done
  49. Return doc
  50. End Function
  51. End Class
  52.