eliza sahoo

eliza sahoo

  • NA
  • 41
  • 0

Working with mail merge word document using VB.NET

Mar 23 2010 8:56 AM
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 & "`")