'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.DrawingImportsSystem.LinqImportsSystem.Collections.GenericImportsSystem.Text.RegularExpressionsImportsGrapeCity.Documents.Word '' This sample loads an existing document, finds all occurrences'' of a certain string in it, And replaces that string with another one,'' also changing the character format of the replacement string.'' This sample Is similar to ReplaceText, with the addition of'' formatting change, And has the same limitation - it only finds'' occurrences of the search string that are completely within a single run.'' See ReplaceTextFmt2 for an example of using PersistentRange in the'' same scenario.PublicClassReplaceTextFmtOldFunctionCreateDocx() AsGcWordDocument'' The document to replace text in:Dim path = IO.Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx")'' The text to findConsttFind = "javascript"'' The replacementConsttRepl = "ArabicaScroll" Dim doc = NewGcWordDocument() doc.Load(path) Dim runs = doc.Body.RunsDim runRanges = NewList(OfRange)(runs.Count)ForEach run In runs runRanges.Add(run.GetRange())Next ForEach rr In runRangesDim str = rr.TextDim matches = Regex.Matches(str, tFind, RegexOptions.IgnoreCase)If matches.Count = 0ThenContinueForEndIf Dim color = rr.ParentRun.Font.Color.RGB rr.Clear()Dim r = rr.Runs.LastDim pos = 0ForEach m In matches r = r.GetRange().Runs.Insert(str.Substring(pos, m.Index - pos), InsertLocation.After) r.Font.Color.RGB = color r = r.GetRange().Runs.Insert(tRepl, InsertLocation.After) r.Font.Color.RGB = Color.Red pos = m.Index + m.LengthNext r = r.GetRange().Runs.Insert(str.Substring(pos), InsertLocation.After) r.Font.Color.RGB = colorIfNotString.IsNullOrEmpty(rr.Runs.First.GetRange().Text) ThenThrowNew Exception("Unexpected")EndIf rr.Runs.First.Delete()Next'' Not strictly necessary but a good practice: runRanges.Clear() '' Add a note at the end of the document doc.Body.Sections.Last.GetRange().Paragraphs.Add($"DsWord replaced '{tFind}' with '{tRepl}' on {Util.TimeNow():R}.") '' DoneReturn docEndFunctionEndClass