Add ( entry ) //Performs the mail merge operation with the dynamic collection document. Add ( entry ) //Retrieves the customer details entry = new DictionaryEntry ( "Orders", "CustomerID = %Customers.CustomerID%" ) commands. Add ( dataTable ) List commands = new List () //DictionaryEntry contain "Source table" (key) and "Command" (value) DictionaryEntry entry = new DictionaryEntry ( "Customers", string. Add ( dataTable ) dataTable = new MailMergeDataTable ( "Orders", GetOrders ()) dataSet. Docx ) //Creates an instance of the MailMergeDataSet MailMergeDataSet dataSet = new MailMergeDataSet () //Creates the mail merge data table in order to perform mail merge MailMergeDataTable dataTable = new MailMergeDataTable ( "Customers", GetCustomers ()) dataSet. GetManifestResourceStream ( "" ), FormatType.
Assembly WordDocument document = new WordDocument () document.
Close () Īssembly assembly = typeof ( App ). ExecuteNestedGroup ( dataSet, commands ) //Saves and closes the Word document instance document. Opens the template document WordDocument document = new WordDocument ( "Template.docx" ) //Creates an instance of the MailMergeDataSet MailMergeDataSet dataSet = new MailMergeDataSet () //Creates the mail merge data table in order to perform mail merge MailMergeDataTable dataTable = new MailMergeDataTable ( "Customers", GetCustomers ()) dataSet.
#Word mail merge from excel nextif example how to
The following code example shows how to perform a nested Mail merge. You can use the “%TableName.ColumnName%” expression for getting the current value of specified column or field from the table. You need to define commands with the table name and expression for linking the multiple data tables (explicit relation data) during nested Mail merge process. The MailMerge class provides various overloads for the ExecuteNestedGroup method to perform Mail merge for nested groups or regions in the Word document. In this template, Employees is the owner group and it has two child groups Customers and Orders. To execute nested mail merge, design your Word document template as follow. You can also predefine the group data that is populated to a merge field. Nested Mail merge operation automatically replaces the merge field with immediate group data. Create template for nested group mail merge This was no big deal, and the end result was quite acceptable.You can perform nested Mail merge with relational or hierarchical data source and independent data tables in a template document. The result being each letter had a (7+1) row table regardless of the number of actual records merged for that branch, some rows remaining empty. Since I knew that none of the branches had more than seven records, my mail merge letter in Word had a table with only 7(+1) rows for the records. The logic was ‘Jump to next record if fieldname ‘branch’ ‘is not blank’ ‘. In the last column, after the mergefield for the employee number I used a ‘Next Record If…’ rule (mergefield NEXTIF). More blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah-etc. Then here was my mail merge letter in Wordīlah-blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah- blah-blah. I did it manually but some simple vba code could do this easily even for data spanning thousands of rows. I inserted a blank line after each value change in col A. Purists may not appreciate but my solution worked for me and saved me time. Sometime the next morning I found a workaround. I actually attempted to adapt a couple of solutions to my needs but gave up when my head started spinning after barely two minutes. Various forums suggested very complicated solutions using elaborate merge field codes.
It appeared that this very elementary and practical problem was not so easy to address with Excel-Word mail merge. After some hair pulling I googled for assistance. At first I thought this would be a cinch. I needed to do a mail merge in Word, using excel as my data source, combining records such that each branch would get one letter with the list for that branch. These were to be allocated to different employees spread over 40 branch offices, and couriered along with a covering letter. The Head Office sent us a couple of hundred new mobile SIMS.