Locked Mail Merge VB.NET 2005 DataSet

  • Tuesday, July 03, 2007 7:20 PM
     
     

    Hi,



    I would like to perform a mail merge between VB.net 2005 and Word 2007 using a dataset from my application, so the user doesn't have to touch word they will just be presented with the mail merged data.


    I have already created my connection to SQL server and populated the dataset but how can I output the dataset to Word.

    I have looked on here and can only find information relating to vb6 not the .net framework examples.  I understand I will need to use Automation to achive this any documents or advice would be great.


    Many thanks


All Replies

  • Friday, July 06, 2007 6:05 AM
     
     Answered

    Rob_1982,

     

    Actually there are many resources on Mail Merge in .NET Framework. I would like to provide you an article in knowledge base that can help you with this kind of questions:

     

    How to automate Word to perform a mail merge from Visual Basic .NET

     

    http://support.microsoft.com/kb/301656/

     

    Automation is a process that allows applications that are written in languages such as Visual Basic .NET to programmatically control other applications. Automation to Word allows you to perform actions such as creating new documents, adding text to documents, and formatting documents. With Word and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using Automation.

     

    Word exposes this programmatic functionality through an object model. The object model is a collection of classes and methods that serve as counterparts to the logical components of Word. For example, there is an Application object, a Document object, and a Paragraph object, each of which contain the functionality of those components in Word. To access the object model from Visual Basic .NET, you can set a project reference to the type library.

     

    This article demonstrates how to set the proper project reference to the Word type library for Visual Basic .NET and provides sample code to automate Word.

     

    You can also search in MSDN Forums by Mail Merge key words.

     

    Word MAil Merge Automation

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1289523&SiteId=1 

     

    I've found a lot of threads on the issue. Thanks again for your question.

     

  • Monday, April 02, 2012 7:21 AM
     
      Has Code

    Hi,

    To mail merge DataTable into Word 2007 document, you can also try this C# / VB.NET Word component that doesn't require Word application (doesn't use Word automation).

    Here is a sample VB.NET Word mail merge code:

    ' 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)