This tip demonstrates how to attach the DB source and populate fields from table to a mail merged word document in VB.NET.
Code Snippet
Dim objWord As New Object objWord = CreateObject("Word.Application") ' Creating a word application Dim objDoc As New Object objDoc = CreateObject("Word.Document") ' Creating a word document object objWord.application.WindowState = 0 ' set the word window in normal state (Const wdWindowStateNormal = 0) objDoc = objWord.Documents.Add(DocsFolder.FullName & "\YOUR WORD DOC NAME") ' Add the mail merged document file Dim NameBeforeWordMerge As String = objWord.ActiveDocument.Name objWord.ActiveDocument.MailMerge.OpenDataSource("YOUR DATABASE PATH", _ Connection:="dsn=DSN NAME; dbq=" & YOUR DATABASE PATH & ";", _ sqlstatement:="select * from `" & tableName & "`") With objWord.ActiveDocument.MailMerge .MailAsAttachment = False .MailAddressFieldName = "" .MailSubject = "" .SuppressBlankLines = True With .DataSource .FirstRecord = 1 ' Const wdDefaultFirstRecord = 1 .LastRecord = -16 ' Const wdDefaultLastRecord = -16 (&HFFFFFFF0) End With .Execute(pause:=True) End With Dim NameAfterWordMerge As String = objWord.ActiveDocument.Name objWord.Documents(NameBeforeWordMerge ).Close(0) ' close the template doc objWord.Documents(NameAfterWordMerge ).Activate() ' activate the mail merged doc after record population
And you are done …