ReplaceText.vb
''
'' This code is part of GrapeCity Documents for Word samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports System.Text.RegularExpressions
Imports GrapeCity.Documents.Word

'' This sample shows how to replace all occurrences of a string
'' in a DOCX document (see JsFrameworkExcerpt) using the
'' RangeBase.Find extension method provided by the
'' RangeBaseFindReplaceExtensions class.
Public Class ReplaceText
    Public Function CreateDocx() As GcWordDocument
        '' The text to find:
        Const tFind = "javascript"
        '' The replacement:
        Const tRepl = "ArabicaScroll"

        '' Load the original JsFrameworkExcerpt document:
        Dim doc = New GcWordDocument()
        doc.Load(Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx"))

        '' Replace all occurrences of the search text in the document body
        '' (note the options that we change from defaults):
        Dim nreplacements = doc.Body.Replace(tFind, tRepl, New FindReplaceOptions(doc) With {.IgnoreCase = True, .RegularExpressions = False})

        '' Add a note at the end of the document:
        doc.Body.Sections.Last.GetRange().Paragraphs.Add(
            $"GcWord replaced {nreplacements} occurrences of '{tFind}' with '{tRepl}' on {DateTime.Now}.")

        '' Done:
        Return doc
    End Function
End Class