ReplaceText.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.Text
  8. Imports System.Text.RegularExpressions
  9. Imports GrapeCity.Documents.Word
  10.  
  11. '' This sample shows how to replace all occurrences of a string
  12. '' in a DOCX document (see JsFrameworkExcerpt) using the
  13. '' RangeBase.Find extension method provided by the
  14. '' RangeBaseFindReplaceExtensions class.
  15. Public Class ReplaceText
  16. Public Function CreateDocx() As GcWordDocument
  17. '' The text to find:
  18. Const tFind = "javascript"
  19. '' The replacement:
  20. Const tRepl = "ArabicaScroll"
  21.  
  22. '' Load the original JsFrameworkExcerpt document:
  23. Dim doc = New GcWordDocument()
  24. doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"))
  25.  
  26. '' Replace all occurrences of the search text in the document body
  27. '' (note the options that we change from defaults):
  28. Dim nreplacements = doc.Body.Replace(tFind, tRepl, New FindReplaceOptions(doc) With {.IgnoreCase = True, .RegularExpressions = False})
  29.  
  30. '' Add a note at the end of the document:
  31. doc.Body.Sections.Last.GetRange().Paragraphs.Add(
  32. $"DsWord replaced {nreplacements} occurrences of '{tFind}' with '{tRepl}' on {Util.TimeNow():R}.")
  33.  
  34. '' Done:
  35. Return doc
  36. End Function
  37. End Class
  38.