Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Text in Word document using Open Xml

Tags:

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.

I am unable to get the hint as to how to access the text from the doument main part?

Any help would be appreciable.

Below is my code till now.

private void CreateSampleWordDocument()     {         //string sourceFile = Path.Combine("D:\\GeneralLetter.dot");         //string destinationFile = Path.Combine("D:\\New.doc");         string sourceFile = Path.Combine("D:\\GeneralWelcomeLetter.docx");         string destinationFile = Path.Combine("D:\\New.docx");         try         {             // Create a copy of the template file and open the copy             File.Copy(sourceFile, destinationFile, true);             using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))             {                 // Change the document type to Document                 document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);                 //Get the Main Part of the document                 MainDocumentPart mainPart = document.MainDocumentPart;                 mainPart.Document.Save();             }         }         catch         {         }     } 

Now how to find certain text and replace the same? I am unable to get via Link, so some code hint would be appreciable.

like image 854
JulyOrdinary Avatar asked Aug 19 '13 14:08

JulyOrdinary


People also ask

Can I open XML with Word?

XML files can be opened in a browser like IE or Chrome, with any text editor like Notepad or MS-Word. Even Excel can be used to open XML files.

How do I edit an Open XML document?

Edit Office Open XML markup in a text editor like Notepad. If you open it in Visual Studio, use Edit >Advanced > Format Document (Ctrl+K, Ctrl+D) to format the package for easier editing.

How do I extract an XML file from a Word document?

To extract the contents of the file, right-click on the file and select “Extract All” from the popup menu. On the “Select a Destination and Extract Files” dialog box, the path where the content of the . zip file will be extracted displays in the “Files will be extracted to this folder” edit box.


1 Answers

Just to give you the idea of how to do it, please try:

  using ( WordprocessingDocument doc =                     WordprocessingDocument.Open(@"yourpath\testdocument.docx", true))             {                 var body = doc.MainDocumentPart.Document.Body;                 var paras = body.Elements<Paragraph>();                  foreach (var para in paras)                 {                     foreach (var run in para.Elements<Run>())                     {                         foreach (var text in run.Elements<Text>())                         {                             if (text.Text.Contains("text-to-replace"))                             {                                 text.Text = text.Text.Replace("text-to-replace", "replaced-text");                             }                         }                     }                 }             }         } 

Please note the text is case sensitive. The text formatting won't be changed after the replace. Hope this helps you.

like image 168
Flowerking Avatar answered Sep 23 '22 12:09

Flowerking