I am creating a C# program that inserts a table inside a word document. Now, I need to insert a table in a certain position in different word documents. All word documents/templates I have has the word <table>
in it where I want to insert the table. For me to be able to insert the table in that particular word document position, I need to get the range of the location of the word <table>
. Is there any possible way to get the range of that particular word inside the word document?
All I can see in the references I have searched are getting ranges using the count of a space, word, sentence, etc. such as the following:
object start = doc.Words[2].Start; //start of the second word
object end = doc.Words[2].End; // start of the third word
Word.Range myRange = doc.Range(ref start, ref end);
Since the number of words inside my word document is dynamic, the code above is not very helpful.
Is there any way to get the range of a specific word inside a word document using C# word interop?
You can use the
Find
object of an empty (or indeed any)Range
to set the bounds of the range to a particular word/string in the document:The
Execute
method has additional parameters (for performing a case-insensitive match, etc).