Find and replace text in word document not working

Hi,

I am using the code sample published in https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace?cs-save-lang=1&cs-lang=csharp to fnd and replace a text in my document but nothing happens, text is not changed. The searched text is found but, not replaced by the new one.

My code:

      1      WordDocument document = new WordDocument("C:\\Users\\Juan Jose\\Documents\\Contrato.doc", FormatType.Docx);
      2     TextSelection selection = document.Find("CONTRATISTA", false, false);
      3     TextBodyPart bodyPart = new TextBodyPart(selection);
      4      document.Replace("TEXTOCAMBIADO", bodyPart, false, true, true);
      5      document.Save(@"C:\Users\Juan Jose\Documents\Borrar\DocumentoXXXXXX.docx", FormatType.Docx);
      6      document.Close();

When I debugg, I can see that bodyPart has the text searched but, at the end, in the document saved, the replacement is not reflected.

How can I check in debugger, the replacement?
Why is not working if the code is the same as the sample exposed in your site?

I added all the references required:
- Syncfusion.Compression.Bass
- Syncfusion.DocIO.Base
- Syncfusion.DocIO.Windows
- Syncfusion.OfficeChart.Base


6 Replies

DB Dilli Babu Nandha Gopal Syncfusion Team August 16, 2018 12:42 PM UTC

Hi Juan, 

Thank you for contacting Syncfusion support. 

In our UG documentation, we have provided code example to replace the text with TextBodyPart of Word document. So, we have used Find API to retrieve the TextBodyPart of the particular text and then replaced the text with it. 

We suggest you to use Replace API of Essential DocIO to replace all text 'CONTRATISTA' with text 'TEXTOCAMBIADO' which illustrated in the following code example. 
WordDocument document = new WordDocument("C:\\Users\\Juan Jose\\Documents\\Contrato.doc", FormatType.Doc); 
// First argument is text to be replaced and next one is replacement text. 
document.Replace("CONTRATISTA", "TEXTOCAMBIADO", false, true); 
document.Save(@"DocumentoXXXXXX.docx", FormatType.Docx); 
document.Close(); 

Please let us know if you have any other questions. 

Regards, 
Dilli babu. 



PG Pon Geetha A J Syncfusion Team August 17, 2018 03:45 AM UTC

Thanks, it works perfect. 

Kind regards, 

Juan J. 



DD Donaat De Vos May 14, 2023 03:09 PM UTC

It doesn't work for me either

We are trying to replace text and then convert it to PDF but replacement seems not working.

here is sample code

 using (FileStream docStream = new FileStream(Path.GetFullPath("path to Docx file"), FileMode.Open, FileAccess.ReadWrite))

            {

                //Loads file stream into Word document

                using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))

                {

                    //Finds the first occurrence of a particular text in the document.

                    TextSelection selection = wordDocument.Find("{{article-desc}}", false, false);

                    //Initializes text body part.

                    TextBodyPart bodyPart = new TextBodyPart(selection);

                    //Replaces a particular text with the text body part.

                    wordDocument.Replace("Adventure Works Cycles", bodyPart, false, true);

                    //Creates file stream.

                    using (FileStream outputFileStream = new FileStream(Path.GetFullPath($"path to save file"), FileMode.Create, FileAccess.ReadWrite))

                    {

                        //Saves the Word document to file stream.

                        wordDocument.Save(outputFileStream, FormatType.Docx);

                    }


                    //Instantiation of DocIORenderer for Word to PDF conversion

                    using (DocIORenderer render = new DocIORenderer())

                    {

                        //Converts Word document into PDF document

                        PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);


                        //Saves the PDF document to MemoryStream.

                        MemoryStream stream = new MemoryStream();

                        pdfDocument.Save(stream);

                        stream.Position = 0;


                        //Download PDF document in the browser.

                        return File(stream, "application/pdf", "Sample.pdf");

                    }

                }

            }



AN Anto Nihil Sahaya Raj Syncfusion Team May 15, 2023 04:37 PM UTC

Hi Donaat,

We have tried to reproduce the reported problem using the given details, but it works properly at our end. For your reference, we have attached the sample application which we tried to reproduce the reported problem at our end. We suggest you to try the attached sample at your end and provide us confirmation whether you are facing issue in this sample.
 It can be downloaded from the below link,
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Find-and-replace1036404960.zip 

If the issue is not reproduced using the attached sample application at your end, then could you please provide the following details for further investigation,
1. Modify the attached sample as an issue reproducible sample application and share us.
2.
Input Word document which used at your end. Because we suspect that the problem might be due to the contents in the input Word document.
3. Syncfusion version used at your side.


Based on the above details, we will check and provide you appropriate solution at the earliest.

Note: If you have any confidential data in your input Word document, please replace with some dummy data and provide us the same. We just need your document to recreate the problem you face. So, once investigated the issue, we will delete the document permanently from our side and do not share with anyone.

Regards,
Anto Nihil S



DD Donaat De Vos replied to Anto Nihil Sahaya Raj May 22, 2023 12:32 AM UTC

Hi  Anto Nihil S,

Thank you for your reply.

I am using the Syncfusion.DocIORenderer.Net.Core version 21.2.5. Also, tried 21.2.4 but still didn't work

I have tried the same version and followed the same code from your sample code project to replace text in Word document. But it still doesn't work for us.

Also, please find the uploaded project here with the docx file that I am using to test find and replace. 

Please let us what's wrong with the code. or what can we do to make it work!

 Thanks


Attachment: FindReplaceAndConvert2PDF_697972a2.zip


AN Anto Nihil Sahaya Raj Syncfusion Team May 22, 2023 06:23 PM UTC

Donaat, from the given details, we have found that your requirement is to replace the text with other text in the Word document. To achieve your requirement, we suggested you pass text to be found (“{{article-desc}}”) as a first parameter and pass text to replace("******** Text changed *******") as a second parameter in the Replace(String, String, Boolean, Boolean) API.


We have modified the sample application to achieve your requirement and it can be downloaded from the attachment.

Please refer to the below link to know more about how to working with find and replace in the Word document,
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace

Regards,
Anto Nihil S


Attachment: FindReplaceAndConvert2PDF_71aba378.zip

Loader.
Up arrow icon