Click here to Skip to main content
15,888,816 members
Articles / Database Development / SQL Server
Article

Creating CSV from SQL Database table attaching it to MS word as datasource and then creating mailmerge document

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
2 Jul 2008CPOL 45.7K   1.6K   33   2
Creating CSV from SQL Database table attaching it to MS word as datasource and then creating mailmerge document

Sample Image - Mailmerge.jpg

Introduction

The code is devided into three parts
1) Creating CSV file
2) Opening CSV file
3) Using CSV file to mailmerge with word document

1) Creating CSV File

CSV file is created from the database table and the file is saved on c: with file name test1.csv.

2) Opening CSV File

This will open the csv file created in step one. It is opened in excel.

3) Mailmerge with word document

For this you will have to first create a word document that will used for merging and save it on c:\ with file name test.doc. The code attach the datasource of test1.csv file to test.doc and also adds field name to word document from datasource.

For using this code you will need to reference of Word and Excel to your project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAnother DOCX mail merge solution Pin
Johnny Glenn13-Mar-12 23:33
Johnny Glenn13-Mar-12 23:33 
Hi,

if you are working with VB.NET DOCX file format, you might also check this C# / VB.NET Word library that works without MS Word installed and has its own .NET Word mail merge functionality, so you can merge DataTable directly without a need to export it to CSV first:
VB
' Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")

' Define DataTable with two columns: 'Name' and 'Surname', and fill it with some data.
' You don't have to do this if you already have a DataTable instance.
Dim dataTable = New DataTable("People")
dataTable.Columns.Add(New DataColumn("Name", GetType(String)))
dataTable.Columns.Add(New DataColumn("Surname", GetType(String)))
dataTable.Rows.Add("John", "Doe")
dataTable.Rows.Add("Fred", "Nurk")
dataTable.Rows.Add("Hans", "Meier")
dataTable.Rows.Add("Ivan", "Horvat")

' Create and save a template document. 
' You don't have to do this if you already have a template document.
' This code is only provided as a reference how template document should look like.
Dim document = New DocumentModel()
document.Sections.Add(
  New Section(document,
    New Table(document,
      New TableRow(document,
        New TableCell(document,
          New Paragraph(document, "Name")),
        New TableCell(document,
          New Paragraph(document, "Surname"))),
      New TableRow(document,
        New TableCell(document,
          New Paragraph(document,
            New Field(document, FieldType.MergeField, "RangeStart:People"),
            New Field(document, FieldType.MergeField, "Name"))),
        New TableCell(document,
          New Paragraph(document,
            New Field(document, FieldType.MergeField, "Surname"),
            New Field(document, FieldType.MergeField, "RangeEnd:People")))))))
document.Save("TemplateDocument.docx", SaveOptions.DocxDefault)

' Load a template document.
document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault)

' Mail merge template document with DataTable.
' Important: DataTable.TableName and RangeStart/RangeEnd merge field names must match.
document.MailMerge.ExecuteRange(dataTable)

' Save the mail merged document.
document.Save("Document.docx", SaveOptions.DocxDefault)

Questionmailmerge in wordperfect using vb.net ? Pin
AJIT KUMAR SUBUDHI16-May-08 20:55
AJIT KUMAR SUBUDHI16-May-08 20:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.