Back to Part 1
In this example I'll use a more dynamic approach that reads customer data from a SQL database and customizes the query before generating the mail merge.
The ASP.NET application pulls in all of the customer data from from the Northwind database and allows paging and sorting of the data via a Grid View Control. Checkbox controls allow selection of records that will be included in the mail merge. Clicking the Mail Merge button selects all of the checked customers and launches Microsoft Word with all of the customer data pre-populated into the mail merge template (shown below).
Download the Microsoft Mail Merge Sample Code - Here
First thing I noticed was that Microsoft no longer includes the sample databases with SQL 2008R2, after a couple of searches I found that you can still download them and run scripts or restore the database files.
After I downloaded the sql2000sampleDb.msi I ran the install. Once the install was complete I opened a command prompt and navigated to C:\SQL Server 2000 Sample Databases. Because my SQL server is installed as the default instance I typed sqlcmd -i instpubs.sql to perform the install, I verified that the Northwind database was now installed on my default SQL instance. Note: if you have a non-default SQL instance you can use the following syntax - sqlcmd -S .\InstanceName -i instpubs.sql
This example uses the Microsoft MSDN sample code for the basic mail merge functions.
Add a using statement for the newly added reference as follows:
using Word = Microsoft.Office.Interop.Word;
I cut the first four lines of the Microsoft code and pasted it at the beginning of the class declaration to ensure proper scope for these variables.
I added GridView and button controls to the page
Next I configured the datasource for the Gridview control and selected the Northwind connection string from the dropdown
Next A select * statement is chosen from the Northwind Customers table on the configure the Select Statement screen
This completes the Gridview datasource
Back to the design view - double click on the button on the form and add the following code to the button click event.
The code will iterate through the gridview control and select only the rows that were checked. I built a very simple customer class to hold the data from the selected rows. This can be customized to include any of the fields from the SQL database but for now I'm only including the ID, ContactName and ContactTitle fields. A Generic Dictionary <T> Collection is used to hold the customer objects and this will be passed to the CreateMailMergeDataFile method.
In the same button click event that you added the snippet above add all of the code in the button1_click event from the MSDN sample code (not all code shown highlighted below)
Next add the remainder of the MSDN sample code into the aspx page just under the public partial class declaration.
Replace the highlighted code in the CreateMailMergeDataFile with the code shown below
Replace highlighted code with this code
I included my project files but you will have to modify the connection string information to your SQL database if it's not on the local machine in the default SQL instance.