Insert data into a table in Microsoft Word

al4629740 used Ask the Experts™
I have attached a word document.  I would like to take the records from my database and insert them into a table in Word.  I also would like to know how to add another row from VB6.  Here is what I have so far...which isn't much

Set objMSWord = CreateObject("Word.Application")
Set myDOc = myWOrd.Documents.Add()

Dim oTable As Word.Table
Dim Para1 As Word.Paragraph

esql = "select * from MyTable
        rec.Open (esql), conn, adOpenStatic, adLockOptimistic
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
It isn't clear why the last row in the table of the sample document has merged cells.
Sub FillTable()
    Dim oTable As Word.Table
    Dim Para1 As Word.Paragraph
    Dim rw As Word.Row
    Dim rec As ADODB.Recordset
    Dim objMSWord As Word.Application
    Dim myDoc As Word.Document
    Dim esql As String
    Dim conn As ADODB.Connection
    Set objMSWord = CreateObject("Word.Application")
    Set myDoc = objMSWord.Documents.Add("C:\MyFolder\")
    Set oTable = myDoc.Tables(1)
    esql = "select * from MyTable"
    rec.Open esql, conn, adOpenStatic, adLockOptimistic, adCmdText
    Do Until rec.EOF
        Set rw = oTable.Rows.Add
        rw.Cells(1).Range.Text = rec.Fields("Objective").Value
        rw.Cells(2).Range.Text = rec.Fields("Description").Value
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial