Replace mergeFields with complex objects

Hello,

I have an already existing Microsoft Word document with mergefields placed through the text, which I later replace with text strings like this:

Dictionary dictionary = new Dictionary();
dictionary.Add("MergeFieldName", "Text to insert");

List enumerableCollection = new List();
enumerableCollection.Add(dictionary);
wordDocument.MailMerge.Execute(enumerableCollection);

This works perfectly fine.

But, what I want to do now is to replace mergefields not with strings, but with bullet point lists. How could I do that?

Thank you for your help.


2 Replies 1 reply marked as answer

HC Hemalatha Chiranjeevulu Syncfusion Team January 5, 2021 02:28 PM UTC

Hi Sistemas,

Thank you for contacting Syncfusion support.

We suspect that your requirement is to set bullet list during mail merge execution. In the Word document, bullet list are not text character which are list format applied to each paragraphs. So, to preserve bullet list for particular line, we have to set list format for that paragraph.

To achieve your requirement, we suggest you use any one of below approaches:

Approach 1:
In DocIO, using MergeField event you can access the merge field during mail merge execution. Using that even, you can able to acess the owner paragraph of merge field and apply bullet list format for that paragraph. Please refer the below code example to do the same at your end:
 
//Loads an existing Word document 
WordDocument wordDocument = new WordDocument(@"Template.docx", FormatType.Docx); 
string[] fieldNames = { "ContactName", "CompanyName" }; 
string[] fieldValues = { "Nancy", "Syncfusion" }; 
//Uses the mail merge events to perform the formatting during runtime 
wordDocument.MailMerge.MergeField += new MergeFieldEventHandler(ApplyListFormat); 
//Performs the mail merge 
wordDocument.MailMerge.Execute(fieldNames, fieldValues); 
//Saves the Word document 
wordDocument.Save("output.docx", FormatType.Docx); 
wordDocument.Close(); 
System.Diagnostics.Process.Start("output.docx"); 
private static void ApplyListFormat(object sender, MergeFieldEventArgs args) 
{ 
//Gets the owner paragraph of current merge field. 
WParagraph paragraph= args.CurrentMergeField.OwnerParagraph; 
//Applies the bullet list style to paragraph 
paragraph.ListFormat.ApplyDefBulletStyle(); 
} 


Approach 2:
Otherwise, you can also prepare the template document with list paragraph (which has merge field). Thereby, it will preserve result with bullet list in the resultant Word document.

Please let us know if you have any other questions.

Regards,
Hemalatha C
 


Marked as answer

DS Dpto. Sistemas January 7, 2021 09:19 AM UTC

Hi,

Worked like a charm. Thank you for your help!



Loader.
Up arrow icon