Mail merge from Excel to Word in C# problem

Hi

I had a problem with the console app it is working fine but there is a message saying system. argumentnullexception. I follow step by step.


this is my code

using Syncfusion.DocIO.DLS;

using Syncfusion.XlsIO;

using System;

using System.Collections.Generic;

using System.Data;

using System.IO;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;


namespace mailmergetest2excel

{

    class Program

    {

        static void Main(string[] args)

        {

            using (ExcelEngine excelEngine = new ExcelEngine())

            {

                //Instantiate the Excel application object

                IApplication application = excelEngine.Excel;


                //Load an existing Excel file into IWorkbook

   Assembly assembly = typeof(Program).GetTypeInfo().Assembly;

   Stream excelStream = assembly.GetManifestResourceStream("Example.xls");

   IWorkbook workbook = application.Workbooks.Open(excelStream,ExcelOpenType.Automatic); //error maseege


                //Get the first worksheet in workbook into IWorksheet

                IWorksheet worksheet = workbook.Worksheets[0];


                //Initialize the DataTable

                DataTable dataTable = new DataTable();


                //Export the data in Excel worksheet into DataTable and assign table name

                dataTable = worksheet.ExportDataTable(worksheet.UsedRange,ExcelExportDataTableOptions.ColumnNames);

                dataTable.TableName = "Customers";


                //Load an existing Word document into WordDocument

                Stream wordStream = assembly.GetManifestResourceStream("mailmergeexample.doc");

                WordDocument wordDocument = new WordDocument(wordStream);


                //Export the data in DataTable into Word document through MailMerge

                wordDocument.MailMerge.Execute(dataTable);


                //Save the Word document and close the instance of WrodDocument

                wordDocument.Save("Output.docx");

                wordDocument.Close();


                System.Diagnostics.Process.Start("Output.docx");

            }

        }

    }

}


4 Replies

RS Ramya Sivakumar Syncfusion Team September 30, 2022 02:24 PM UTC

Hi


Greetings from Syncfusion.


Please include the Excel document in the project and set the Build Action property of the document as Embedded Resource. And please access the file using NameSapce.FileName format.


Kindly use the following code snippet and let us if it helps.


Code snippet:

Stream excelStream = assembly.GetManifestResourceStream("mailmergetest2excel.Example.xls");


Regards,

Ramya



?? ???? October 2, 2022 10:38 AM UTC

it is the same error masseage



?? ???? replied to Ramya Sivakumar October 2, 2022 10:41 AM UTC

same error



KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team October 3, 2022 10:03 AM UTC

Hi


Yes. This is the same System.ArgumentNullException raised when the input document being loaded in assembly.GetManifestResourceStream is not made an embedded resource in project level.


Kindly try the provided suggestion and let us know if this helps. If the issue still persists, there might be issue in file parsing. Kindly share the input documents you are using at your end in this case, to investigate the query further.


You can share the documents to [email protected] by mentioning the forum ID, if you are not comfortable sharing them here in public.


Regards,

Keerthi.


Loader.
Up arrow icon