'''' This code is part of Document Solutions for Word demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.DrawingImportsSystem.TextImportsSystem.Text.RegularExpressionsImportsGrapeCity.Documents.Word '' NOTE: this sample is obsolete, use the RangeBase.Replace()'' @extension method instead (see @{ReplaceText}).'''' This sample loads an existing document, finds all occurrences'' of a certain string in it, And replaces that string with another one.'' Note that this code only finds occurrences of the search string'' that are completely within a single run. To find strings spanning'' two Or more runs (e.g. if different parts of the string have'' different formatting) will require a more complex searching logic.'' For a similar sample that also changes the formatting of the'' replacement, see ReplaceTextFmt.PublicClassReplaceTextOldPublicFunctionCreateDocx() AsGcWordDocument'' The document to replace text in:Dim path = System.IO.Path.Combine("Resources", "WordDocs", "JsFrameworkExcerpt.docx")'' The text to findConsttFind = "javascript"'' The replacementConsttRepl = "ArabicaScroll" Dim doc = NewGcWordDocument() doc.Load(path) '' Loop over all texts in the document bodyForEach text In doc.Body.TextsIfTypeOf (text) IsBreakThenContinueForEndIf Dim str = text.ValueDim matches = Regex.Matches(str, tFind, RegexOptions.IgnoreCase)If matches.Count = 0ThenContinueForEndIf Dim sb = NewStringBuilder()Dim pos = 0ForEach m In matches sb.Append(str.Substring(pos, m.Index - pos)) sb.Append(tRepl) pos = m.Index + m.LengthNext sb.Append(str.Substring(pos)) text.Value = sb.ToString()Next '' 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