[Word] Finding and replacing a block of text for multiple entries

Hello,

we have a routine that compiles a contract set of data (we usually call them tags) and then replace the respective tag name from a Word document. Note that the text on the Word template are not merge fields, just plain text.

Currently we are doing a replacement routine on the internal XML document file, and are trying to find a more effective and consistent way of doing this process.

I made a sample console app (no text is generated, just the output file is opened on completion) up to the point I was able to make do what we need, however I did not found how to "copy and paste" the found text blocks to do multiple replacements, as it is the case when the contract if for more than one customer. Example:

{CUSTOMERS}Name: “{CUSTOMER_NAME}”, with ID “{CUSTOMER_ID}”

{/CUSTOMERS}

(the line break is intentional as to put each customer in a separate line)

In my example, the data of 2 customers must be applied to the groups.

I would like to know how to, after finding a customer group, pick that text block and do a paste-and-replace on the document for the amount of customers given.

Also, in the example I use data "pages" because the clients have the ability to generate multiple contracts as one document, so a "page" contains the data for each contract (in my example I left as 3 pages to check if there's no erroneous replacement)


Thanks in advance.





Attachment: TesteSyncfusionDOCX_5d40a1e1.zip

5 Replies 1 reply marked as answer

HC Hemalatha Chiranjeevulu Syncfusion Team June 29, 2021 04:33 PM UTC

Hi Fernando,

Thank you for your patience.

From the details, we have found that your requirement is to find set of date and replace it with desired text in the Word document. To achieve this, we suggest you to use mail merge functionality in DocIO to replace the content dynamically in the Word document. We have modified the input Word document to achieve your requirement.

In the modified input document, we have inserted the merge field in the place of text where we need to replace. We have prepared the sample application to achieve your requirement and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/166699/ze/FindAndReplace-2091772623

In the above sample, we have done the following things:
1. Load the template Word document
2. Get the data to perform mail merge
3. Perform mail merge
4. Save and close the Word document

Please find the generated output Word document from the below link:
https://www.syncfusion.com/downloads/support/forum/166699/ze/Result-354084546

Please refer the below links to know more about how to work with mail merge using Essential DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-mail-merge
https://github.com/SyncfusionExamples/Mail-Merge-Examples

If we misunderstood any of your requirement, please share the expected output Word document. Thereby, we will check on your scenario and will try any other feasibility to achieve your requirement.

Please let us know if you have any other questions.

Regards,
Hemalatha C



FR Fernando Rodrigues June 29, 2021 09:43 PM UTC

Thanks for the response.

The use o merge fields seems to almost solve the contract generation, however we require that the customers from a contract to be on the same document. This is signaled by the "{CUSTOMERS}[...]{/CUSTOMERS}" sections on the template document. Based on your example, "Customer#1" and "Customer#2" should be on the same document copy.

With mail merge in mind I came across this page that suggest using ExecuteNestedGroup to achieve that. Now what I am not sure how to do is how we setup a "blank" data source (I think a sample line for each group would be enough) so our clients can make use of the expected fields while making the template document. For this example, I believe the hierarchy would be:


  • Contract
    • Customers
    • [Other grouped data]
Thanks in advance.


HC Hemalatha Chiranjeevulu Syncfusion Team June 30, 2021 12:44 PM UTC

Hi Fernando,

Thank you for your update.

As per your requirement, we have prepared the sample application. Please find the modified sample from the below link
https://www.syncfusion.com/downloads/support/forum/166699/ze/FindAndReplace_Modified-1064030578

If we misunderstood any of your requirement, please share the expected output Word document. Thereby, we will check on your scenario and will try any other feasibility to achieve your requirement.

Please let us know if you have any other questions.

Regards,
Hemalatha C



FR Fernando Rodrigues replied to Hemalatha Chiranjeevulu July 1, 2021 02:05 AM UTC

Thank you,

With your example I was able to setup a test template document.

For future reference:

One of the issues I had was that changing the text on the merge field actually doesn't change the name of field itself. You need to right click the field and use "Edit Field..." to really change it's name.

Now I have done it so it removes empty groups too, as it is possible to some contracts have a group and another one not have the corresponding data.

Attached is a working example.


Attachment: WorkingContractMerge_3922a1eb.zip

Marked as answer

LB Lokesh Baskar Syncfusion Team July 1, 2021 12:42 PM UTC

Hi Fernando,

Thank you for your update.

We are glad to know that your problem has been fixed.

Please let us know if you have any other questions and we will be happy to assist you as always.

Regards,
Lokesh B


Loader.
Up arrow icon