Hello! There is a code that searches for text in Word'e and returns the Range object accordingly (I can iterate over paragraphs and output), but how can I proceed? If I want to post the following lines after this text, but I am given the opportunity to control only one paragraph, and how can I move to the next after it? Or how to 'highlight the desired range' from the found Range - to reach the end, SetRange requires positions, how to get them?

private Word.Range FindInWord() { object stringToFindObj = textBox1.Text; Word.Range wordRange; bool rangeFound; object _missingObj = Type.Missing; for (int i = 1; i <= doc.Sections.Count; i++) { wordRange = doc.Sections[i].Range; Word.Find wordFindObj = wordRange.Find; object[] wordFindParameters = { stringToFindObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj }; rangeFound = (bool)wordFindObj.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, wordFindObj, wordFindParameters); if (rangeFound) { return wordRange; } } return null; } 

    1 answer 1

    The question is not quite clear, but as I understand it the method should be approximately the same for your needs:

     private Word.Range FindInWord(out Section currentSection, out int index) { object stringToFindObj = textBox1.Text; Word.Range wordRange; bool rangeFound; object _missingObj = Type.Missing; for (int i = 1; i <= doc.Sections.Count; i++) { wordRange = doc.Sections[i].Range; Word.Find wordFindObj = wordRange.Find; object[] wordFindParameters = { stringToFindObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj, _missingObj }; rangeFound = (bool)wordFindObj.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod, null, wordFindObj, wordFindParameters); if (rangeFound) { currentSection = doc.Sections[i]; index = i; return wordRange; } } currentSection = null; index = -1; return null; } 

    The code did not check it is not possible now and you need to remember to process the parameters after executing the function.