While my favorite way to write these types of algorithms is to use LINQ to XML, to make this code more widely applicable, I used System.Xml.XmlDocument, which is an implementation of XML DOM. This makes it easier to translate this code to a variety of other platforms, such as PHP or Java. After the algorithm replaces the single-character runs with a new run containing the replacement text, then the algorithm coalesces adjacent runs with the same formatting into a single run. If you replace 'include' with 'do not include', then the sentence should be formatted like this:. A reoccurring question around Open XML is how to search and replace text in a word-processing document. There have been several attempts at presenting example code to do this, however, until now I have not seen any examples that correctly implement this. This post presents some example code that implements a correct algorithm to search and replace text. The code searches and replaces text in the main document part, all headers, all footers, the endnote part, and the footnote part. On the Insert tab, the galleries incl ude items. There are a few additional notes worth mentioning about this algorithm. Concatenate all text in a paragraph into a single string, and search for the search string in the concatenated text. If the search text is found, then continue with the following steps. Browse by categories: Open XML blog posts, screen-casts, MSDN articles, and MSDN documentation. After breaking runs of text into multiple runs of single characters, it is then pretty easy to iterate through the runs looking for a string of runs that match the characters in the search string. The replaced text takes on the formatting of the 'i' character of include, which was bolded. If revision tracking is turned on for a document, the correct functionality would be to create the revision tracking markup, which is beyond the scope of this example. If revision tracking is turned on, the example code throws an exception. Even though the search text spans runs, the algorithm should find the text and replace it. The next challenge is to define exactly the semantics of searching and replacing text if the text that you are searching for spans runs with different formatting. In short, the replaced text takes on the run formatting of the run that contains the first character of the search string. An example makes this clear. In the following sentence, the first four characters of the word 'include' are bolded: Here is a short screen-cast that walks through the algorithm and the code. However, there is another approach that we can take that is pretty simple, easy to test, and yields the correct results in all cases. The algorithm consists of: After splitting all runs into multiple runs of a single character each, the markup looks like this:. If the algorithm finds a string of runs that match the search string, then it inserts a new run into the document. This new run contains the run properties of the first run in the string of runs that match the search string. In addition, the algorithm deletes the set of single-character runs that matched the search string. This process is repeated until no strings of runs are found that match the search string. Iterate through all runs in the paragraph, and break all runs into runs of a single character. There are a variety of special characters, such as carriage return, hard tab, break, and the non-breaking hyphen character. Normally, these special characters will coexist in runs with text elements. When breaking runs into runs of a single character, these special characters should also be placed into their own run. At the end of this process, no run will contain more than a single character, whether it is a character of text, or one of the special characters that is represented by an XML element. Finally, the algorithm iterates through the runs, coalescing adjacent runs with identical formatting. The first challenge is handle the case when the text you are searching for spans runs with different formatting. A simple example will demonstrate the problem. You want to replace 'Hello World' with 'Hi World'. If, in the document, the word 'World' is bolded, then the markup will look something like this: On the Insert tab, the galleries do not include items. Please note the text is case sensitive. The text formatting won't be changed after the replace. Hope this helps you. Discuss the workings and policies of this site. I have created a docx file from a word template, now I am accessing the copied docx file and want to replace certain text with some other data. When your Word file has textboxes in it, his solution would not work. Because textbox has TextBoxContent element so it will not appear at foreach loop of Run s. Replace Text in Word document using Open Xml. Just to give you the idea of how to do it, please try:. @flowerking: If you have a a few mins could you help out with this? stackoverflow.com/questions/26307691. I had asked you to give answer to my previous question as well as your link helped me, so post answer there as well. // Create a copy of the template file and open the copy. Start here for a quick overview of the site. Now how to find certain text and replace the same? I am unable to get via Link, so some code hint would be appreciable. Learn more about hiring developers or posting ads with us. I am unable to get the hint as to how to access the text from the doument main part?. this only replaces text in one run. However, text may be chopped up in different runs, which fisrt must be concatenated before replacement can be done. Detailed answers to any questions you might have.