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.
Unfortunately, activation email could not send to your email. Please try again.

Exceptions with MailMerge and dynamic types

Thread ID:

Created:

Updated:

Platform:

Replies:

124908 Jul 7,2016 03:01 PM Nov 27,2017 04:12 AM Windows Forms 6
loading
Tags: DocIO
Chris Graf
Asked On July 7, 2016 03:01 PM

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: 





Chris Graf
Replied On July 7, 2016 03:07 PM

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 !?


Ayswarya Krishna Kumar [Syncfusion]
Replied On July 8, 2016 08:19 AM

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


Ootam Seewoogoolam
Replied On November 27, 2017 12:50 AM

Hi, 

Has this issue been resolved?

Sethumanikkam Yogendran [Syncfusion]
Replied On November 27, 2017 03:52 AM

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


Ootam Seewoogoolam
Replied On November 27, 2017 04:01 AM

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


Sethumanikkam Yogendran [Syncfusion]
Replied On November 27, 2017 04:12 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;