Skip to content

Commit 547fc31

Browse files
Modified the find and apply bold to replaced content code logic properly
1 parent e371b68 commit 547fc31

File tree

1 file changed

+7
-14
lines changed
  • Find-and-Replace/Find-and-apply-bold-to-replaced-content/.NET/Find-and-apply-bold-to-replaced-content

1 file changed

+7
-14
lines changed

Find-and-Replace/Find-and-apply-bold-to-replaced-content/.NET/Find-and-apply-bold-to-replaced-content/Program.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,14 @@ static void Main(string[] args)
2525
int paraIndex = body.ChildEntities.IndexOf(paragraph);
2626
// Replace the selected text with the replace content.
2727
document.Replace(selection.SelectedText, "Adventure Works Cycles", true, true);
28-
// Iterate the body from the replaced paragraph.
29-
for (int i = paraIndex; i < body.Count; i++)
28+
// Search for the word within the current paragraph.
29+
TextSelection replacedSelection = paragraph.Find("Works", true, false);
30+
if (replacedSelection != null)
3031
{
31-
// Access each paragraph in the body.
32-
WParagraph para = body.ChildEntities[i] as WParagraph;
33-
// Search for the word within the current paragraph.
34-
TextSelection replacedSelection = para.Find("Works", true, false);
35-
if (replacedSelection != null)
36-
{
37-
// Get the selected text as a single text range.
38-
WTextRange replacedTextRange = replacedSelection.GetAsOneRange();
39-
// Apply bold formatting to the text range.
40-
replacedTextRange.CharacterFormat.Bold = true;
41-
break;
42-
}
32+
// Get the selected text as a single text range.
33+
WTextRange replacedTextRange = replacedSelection.GetAsOneRange();
34+
// Apply bold formatting to the text range.
35+
replacedTextRange.CharacterFormat.Bold = true;
4336
}
4437
// Save the modified document to the specified output path.
4538
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))

0 commit comments

Comments
 (0)