Answer the question
In order to leave comments, you need to log in
Word bookmarks
Good afternoon.
Please tell me how you can replace bookmarks with text in a Word file saved in XML format. Bookmarks in this case are denoted by the bookmarkStart and bookmarkEnd tags. Instead of replacing, you can also simply insert text, it does not matter. After the replacement, it is necessary to write the changes to a new file and prompt the user to save it. I would be very grateful for the working code in C#.
Answer the question
In order to leave comments, you need to log in
lazy at all?
To insert text in a range
1. Specify a range at the beginning of a document and insert the text New Text.
The following code example can be used in a document-level customization.
object start = 0;
object end = 0;
Word.Range rng = this.Range(ref start, ref end);
rng.Text = “New Text”;
The following code example can be used in an application-level add-in. This code uses the active document.
Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
rng.Text = “New Text”;
2. Select the Range object, which has expanded from one character to the length of the inserted text.
rng.Select();
Replacing Text in a Range
If the specified range contains text, all text in the range is replaced with the inserted text.
1. Create a Range object that consists of the first 12 characters in the document.
The following code example can be used in a document-level customization.
object start = 0;
object end = 12;
Word.Range rng = this.Range(ref start, ref end);
The following code example can be used in an application-level add-in. This code uses the active document.
Word.Range rng = this.Application.ActiveDocument.Range(0, 12);
2. Replace those characters with the string New Text.
rng.Text = “New Text”;
3. Select the range.
rng.Select();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question