We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Exceptions with MailMerge and dynamic types

I'm trying to call MailMerge.ExecuteNestedGroup on a WordDocument with data that is deserialized from JSON (using Json.Net).

Here is a sample code which demonstrates the issue :

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.IO;
 
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
 
public static class Prog
{
    public static void Main(string[] args)
    {
        // Dynamic
        dynamic modelDynamic = new
        {
            IndividualName = "Bob",
            IndividualAddress = new
            {
                City = "Montreal"
            }
        };
 
        MergeWordAndModelIntoPdf(modelDynamic); // Ok
 
        // Dictionary
        var modelDictionary = new Dictionary<stringobject>
        {
            {"IndividualName""Bob Daule"},
            {"IndividualAddress"new Dictionary<string,object>{{"City","Montreal"}}}
        };
 
        MergeWordAndModelIntoPdf(modelDictionary); // Fails
 
        // Expando Object
        dynamic modelExpandoObject = new ExpandoObject();
        modelExpandoObject.IndividualName = "Bob";
        modelExpandoObject.IndividualAddress = new ExpandoObject();
        modelExpandoObject.IndividualAddress.City = "Montreal";
 
        MergeWordAndModelIntoPdf(modelExpandoObject); // Fails
 
        Console.ReadLine();
    }
 
    private static void MergeWordAndModelIntoPdf(object model)
    {
        const string InputDocument = "Order2.docx";
        const string OutputDocument = "out.pdf";
 
        using (var wordDoc = new WordDocument(File.OpenRead(InputDocument)))
        {
            wordDoc.MailMerge.ExecuteNestedGroup(new MailMergeDataTable("Model"new[] { model }));
 
            using (var converter = new DocToPDFConverter())
            {
                using (var pdfDoc = converter.ConvertToPDF(wordDoc))
                {
                    pdfDoc.Save(OutputDocument);
                }
            }
 
            Process.Start(OutputDocument);
        }
    }
}

And my Word document is like this:

«BeginGroup:Model»

Name: «IndividualName»

«BeginGroup:IndividualAddress»

«Line1»

«EndGroup:IndividualAddress»

«EndGroup:Model»


When I use modelDynamic it works but I could not find a way to deserializing into a similar object which works...

When I use modelDictionary I get the following exception:

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Syncfusion.DocIO.Base
  StackTrace:
       at Syncfusion.DocIO.DLS.MailMerge.GetFieldValue(String fieldName, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.GetFieldValue(IWMergeField field, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.UpdateMergeFieldValue(WMergeField mergeField, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroupForSelection(WTextBody textBody, Int32 itemStart, Int32 itemEnd, Int32 pItemStart, Int32 pItemEnd, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.OnBodyGroupFound(IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.CheckItem(ParagraphItem item)
       at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.FindInBodyItems(BodyItemCollection bodyItems)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroupForSelection(WTextBody textBody, Int32 itemStart, Int32 itemEnd, Int32 pItemStart, Int32 pItemEnd, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.OnBodyGroupFound(IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.CheckItem(ParagraphItem item)
       at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.FindInBodyItems(BodyItemCollection bodyItems)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroup(WSection section, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(String tableName)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(MailMergeDataTable dataTable)
       at ConsoleApplication3.Prog.MergeWordAndModelIntoPdf(Object model) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 56
       at ConsoleApplication3.Prog.Main(String[] args) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 36
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 



When I use modelExpandoObject I get the following exception:

System.NotSupportedException was unhandled
  HResult=-2146233067
  Message=Specified method is not supported.
  Source=System.Core
  StackTrace:
       at System.Dynamic.ExpandoObject.<GetExpandoEnumerator>d__46.System.Collections.IEnumerator.Reset()
       at Syncfusion.DocIO.DLS.DataTableEnumerator..ctor(MailMergeDataTable table)
       at Syncfusion.DocIO.DLS.MailMerge.GetEnum(String tableName)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroupForSelection(WTextBody textBody, Int32 itemStart, Int32 itemEnd, Int32 pItemStart, Int32 pItemEnd, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.OnBodyGroupFound(IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.CheckItem(ParagraphItem item)
       at Syncfusion.DocIO.DLS.MailMerge.GroupSelector.FindInBodyItems(BodyItemCollection bodyItems)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteGroup(WSection section, IRowsEnumerator rowsEnum)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(String tableName)
       at Syncfusion.DocIO.DLS.MailMerge.ExecuteNestedGroup(MailMergeDataTable dataTable)
       at ConsoleApplication3.Prog.MergeWordAndModelIntoPdf(Object model) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 56
       at CallSite.Target(Closure , CallSite , Type , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
       at ConsoleApplication3.Prog.Main(String[] args) in c:\Users\cgraf\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 44
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 





6 Replies

CG Chris Graf July 7, 2016 07:07 PM UTC

I can't edit my post, so please note:

In the word template Line1 is actually City (bad copy past).

And my question would be:  How can I pass dynamic data (deserialized from JSON) into MailMerge without it throwing an Exception !?



AK Ayswarya Krishna Kumar Syncfusion Team July 8, 2016 12:19 PM UTC

Hi Chris,

Thank you for contacting Syncfusion support.

A support incident to track the status of the reported exception issue has been created under your account. Please log on to our support website to check for further updates.

https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents

Regards,
Ayswarya



OS Ootam Seewoogoolam November 27, 2017 05:50 AM UTC

Hi, 

Has this issue been resolved?


SY Sethumanikkam Yogendran Syncfusion Team November 27, 2017 08:52 AM UTC

Hi Ootam,

Thank you for your update.

Has this issue been resolved?
Yes. We are glad to inform you that the reported issue has been already fixed and the fix is included in our 2016 Volume 2 SP 1 v14.2.0.28 release.

You
can download our latest Essential Studio 2017 Volume 4 Release v15.4.0.17 from the following link.
https://www.syncfusion.com/forums/134428/essential-studio-2017-volume-4-release-v15-4-0-17-is-available-for-download

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y



OS Ootam Seewoogoolam November 27, 2017 09:01 AM UTC

Thanks for the quick reply.. I was able to get it working.. 



SY Sethumanikkam Yogendran Syncfusion Team November 27, 2017 09:12 AM UTC

Hi Ootam,

Thank you for your update.

We are glad to know that your issue has been fixed. Please get back to us if you need any further assistance. We are happy to assist you as always.

Regards,
Sethumanikkam.Y


Loader.
Live Chat Icon For mobile
Up arrow icon