How to replace the text in a document


I need to be able to take an existing Word document and replace all the text. I need to start from the existing document because it may have comments that need to be preserved. In short, what I think I need to do is:

  1. Open the existing document
  2. Delete the existing text of the document (this is where I’m stuck)
  3. Insert the new text

Any help in where to start would be greatly appreciated.



Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information about your query along with code.


Attached is a zip with two documents (Before.docx and After.docx). The Before.docx file has track changes turned on with two comments. The After.docx is the same text except a paragraph was removed (noted in the document). The tracked changes and comments were kept in the After.docx.

I need to run some tests to determine what happens when the tracked changes and comments from the original document are affected when the text is programatically updated so I can explain the potential side-affects to my sponsor. (27.2 KB)

Thanks in advance.



Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. In your case, we suggest you please accept all changes of both documents and compare them to check the difference. Please refer to the following article:
How to Compare Two Word Documents


I don’t want to compare the documents, I want to replace the text of the document. i.e. Open the document, select all text and delete it, then insert the updated text.




Thanks for your inquiry. You can find the desired text in the document and replace it with new text. Please read the following article. Hope this helps you.
Find and Repalce

Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "Before.docx");

String findText = @"Fermentum gravida dolor diam gravida aliquet ad leo a natoque vel a ridiculus erat sociis senectus arcu accumsan primis mus a. Facilisis vestibulum nunc a in dui a non dui a proin consectetur diam diam placerat. Egestas curae a etiam taciti consectetur habitasse nec a id at vestibulum morbi dapibus orci etiam scelerisque ante vestibulum vivamus dui turpis. Adipiscing hac habitant parturient conubia quisque augue in a a proin adipiscing malesuada praesent placerat dignissim scelerisque hac a hendrerit viverra semper.";
int replace = doc.Range.Replace(findText, "Paragraph Removed.", new FindReplaceOptions());

doc.Save(MyDir + "17.8.docx");