private void button1_Click(object sender, RibbonControlEventArgs e) { Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument; ClsTybeScript c = new ClsTybeScript(doc); c.applytybescript(new Regex(@"Chapter\s+(\d+)\:\s([^\n]+)", RegexOptions.IgnoreCase)); }
class ClsTybeScript { Word.Document doc = null; public ClsTybeScript(Word.Document doc) { this.doc = doc; } public void applytybescript(Regex regex) { Word.Range t= doc.Content.Duplicate; t.Collapse(Word.WdCollapseDirection.wdCollapseStart); foreach(Match match in regex.Matches(doc.Content.Text)) { t.Find.ClearFormatting(); t.Find.Text = match.Value; //t.Find.MatchWildcards = true; if (t.Find.Execute()) { if (t.InRange(doc.Content)) { t.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBrightGreen; t.Find.Replacement.Text = @"$1^|$2"; } } t.Collapse(Word.WdCollapseDirection.wdCollapseEnd); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)