ReplaceTextFmt.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports System.Linq
Imports System.Collections.Generic
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.Replace extension method provided by the
'' RangeBaseFindReplaceExtensions class.
'' This sample Is similar to ReplaceText, with the addition of
'' also updating the replacement's style.
Public Class ReplaceTextFmt
    Public Function CreateDocx() As GcWordDocument
        '' The document to replace text in:
        Dim path = IO.Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx")
        '' The text to find
        Const tFind = "javascript"
        '' The replacement
        Const tRepl = "ArabicaScroll"
        '' Name for the replacement's style:
        Const styleName = "my new style"

        '' Load the source document:
        Dim doc = New GcWordDocument()
        doc.Load(path)

        '' The New style for replaced text:
        Dim style = doc.Styles.Add(styleName, StyleType.Character)
        style.Font.Color.RGB = Color.Red

        '' Replace text And replacements' style:
        Dim nreplacements = doc.Body.Replace(tFind, tRepl,
            New FindReplaceOptions(doc) With
            {
                .IgnoreCase = True,
                .ReplacedCallback = Sub(args_) args_.ReplacedRange.Runs.ToList().ForEach(Sub(r_) r_.Style = style)
            })

        '' Add a note at the end of the document
        doc.Body.Paragraphs.Add(
            $"DsWord replaced {nreplacements} occurrences of '{tFind}' with '{tRepl}' on {Util.TimeNow():R}.")

        '' Done
        Return doc
    End Function
End Class