Mail Merge Format Fail

1 Answer 69 Views
General Discussions
Coopbase
Top achievements
Rank 1
Coopbase asked on 09 May 2021, 11:45 PM

Hello,

 

I have a Word document in docx format and mail merge with Excel in xls format.

The following are mergefields that rendered properly in Word but not when I do mail merge in my code.

{ MERGEFIELD TODAY \@ “MM/dd/yy” }

{ MERGEFIELD BAL \# ####,0.00 }

The following code:


                    using (Stream stream = new FileStream(docxPath +docxFilename, FileMode.Open))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        byte[] bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);

                        templateDocument = new DocxFormatProvider().Import(bytes);
                        IEnumerable mailMergeSource = this.GetMailMergeDataSource(worksheet);
                        mergeDocument = templateDocument.MailMerge(mailMergeSource);
                        this.SaveFile(mergeDocument);
                    }

Above is the main code that do mailmerge

The GetMailMergeDataSource is give below:


        private IEnumerable GetMailMergeDataSource(Worksheet worksheet)
        {
            List<DynamicDataObject> mailMergeSource = new List<DynamicDataObject>();
            string[] fieldNames = new string[worksheet.UsedCellRange.ColumnCount];
            int columnCount;
            int rowCount;
            CellSelection selection;

            for (columnCount = 0; columnCount < worksheet.UsedCellRange.ColumnCount; columnCount++)
            {
                selection = worksheet.Cells[0, columnCount];
                fieldNames[columnCount] = selection.GetValue().Value.RawValue.ToString();
            }

            for (rowCount = 1; rowCount < worksheet.UsedCellRange.RowCount; rowCount++)
            { 
                DynamicDataObject data = new DynamicDataObject();
                for (columnCount = 0; columnCount < worksheet.UsedCellRange.ColumnCount; columnCount++)
                {
                    selection = worksheet.Cells[rowCount, columnCount];
                    data.Set(fieldNames[columnCount], selection.GetValue().Value.RawValue);
                }

                if (columnCount > 0)
                    mailMergeSource.Add(data);
            }

            return mailMergeSource;
        }

DynamicDataaObject is derived from Telerik own's sample document-processing-sdk-master > WordsProcessing

the SaveFile is below:


        private void SaveFile(RadFlowDocument document)
        {
            using (Stream stream = File.OpenWrite(newDocxPath + newDocxFilename))
            {
                DocxFormatProvider formatProvider = new DocxFormatProvider();

                DocxExportSettings exportSettings = new DocxExportSettings
                {
                    AutoUpdateFields = true,
                    InvalidDocumentAction = InvalidDocumentAction.ThrowException
                };
                formatProvider.ExportSettings = exportSettings;
                formatProvider.Export(document, stream);

            }


        }

It works. It merged into another docx document but the Merge Field format failed.  But to do the same with Word merge, the resulting file shows proper format.

Let me know if you need full working sample that demo the problem.

 

Thank,

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 11 May 2021, 09:19 AM

Hi,

Thank you for the feedback and for sharing the code snippets with us.

It seems you are hitting a known issue related to the formatting applied on mail merged fields. We have an item logged in our backlog to fix it: WordsProcessing: Formatting (Date, Numeric, and General) of merge field is not respected. Please, make sure to follow the item so you can be notified when its status changes.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Coopbase
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or