VB 6.0 - Creating and accessing a word table via VB6

Asked By Peter Lupton on 02-Feb-07 11:16 AM
I want to create a word document that contains a table and then puts extracted info into various cells within the table. I can create the word document and list the info I want but I can't create the table and put the info into cells.

using DocumentObject.Tables.Add collection - sundar k replied to Peter Lupton on 02-Feb-07 11:36 AM

try this code!

Function MakeWordTable(stDocument As String, stNewDocument As String)
Dim objWord As Word.Application
Dim objWordDoc As Word.Document
Dim objTable As Word.Table
Dim objRange As Word.Range
Dim x As Integer, y As Integer

Set objWord = CreateObject("Word.Application")
Set objWordDoc = objWord.Documents.Open(stDocument)
'create a range for a bookmark, you need a bookmark to create table
objRange = objWordDoc.Goto(wdGoToBookmark, , , "Bookmark1")

Set objTable = objWordDoc.Tables.Add(objRange, 3, 5)

For x = 1 To 3
For y = 1 To 5
objTable.Cell(x, y).Range.InsertAfter "Cell " & x & "," & y
Next y
Next x

objWordDoc.SaveAs stNewDocument

Set objTable = Nothing
Set objRange = Nothing
Set objWordDoc = Nothing
Set objWord = Nothing

End Function

you can also refer to below link for lot of sample codes,

Automating Word Tables for Data Insertion and Extraction


Re ::Creating and accessing a word table via VB6.0 - Shailendrasinh Parmar replied to Peter Lupton on 18-Nov-08 07:05 AM

See the following articles for creating Table in Word document from VB 6.0 application




Hope this helps.