Please show me how to programatically add at least two tables in the word document. That is the essence of my question
Main Topics
Browse All TopicsExperts,
I am creating strings from data fetched from MS Access, and using convertotable, want to convert each of the strings into a table and present the tables one after the other in a word doc. The code below shows creation of one table (obviously I'm not staticially creating it, but converting it from a string created from a DB query). With reference to this code, can you show me how to display two or more tables in the same word doc
Thanks!
DoctorNash
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim sTemp As String
Set oWord = CreateObject("word.applica
oWord.Visible = True
Set oDoc = oWord.Documents.Add
Set oRange = oDoc.Range
sTemp = "Name" & vbTab & "Feeling" & vbCrLf & "DocNash" & vbTab & "Happy"
oRange.Text = sTemp
oRange.ConvertToTable vbTab, , , , wdTableFormat3DEffects3
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi Doctornash
Looks like you want two or more tables in a document to contain information extracted from a db. This will set up a series of tables with one row and four columns for each record in the recordset processed, and move in the first four fields of the recordset into the row.
The tble.style code wil create a table with full, single cell borders.
dim odoc as word.document
dim tbl as wrd.table
rs.movefirst
while not rs.eof = true
'create tbl
Set tbl = ActiveDocument.Tables.Add(
4, DefaultTableBehavior:=wdWo
wdAutoFitFixed)
With tbl
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
tbl.Cell(1, 1).Range.Text = rs.fields(0) 'move in rs field data
tbl.Cell(1, 2).Range.Text = rs.fields(1)
tbl.Cell(1, 3).Range.Text = rs.fields(2)
tbl.Cell(1, 4).Range.Text = rs.fields(3)
Selection.MoveDown Unit:=wdLine, Count:=1 'move cursor out of table
Selection.TypeParagraph 'skip 1 line
set tbl = nothing
rs.movenext
wend
hope this'll help
dmang
Thanks guys, but I have not found either of the suggestions provided here truly helpful. I ended up solving it myself as follows:
Sub MakeDoc()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange As Word.Range
Dim sTemp As String
Dim p As Integer
Dim oTable As Table
Dim X As Integer
Dim Y As Integer
'variables for rendering of Tables:
Dim NumT As Integer 'the number of the table we have to render eg if there are 5 tables, they would be numbered 1-5
Dim NumColsT As Integer 'the number of columns the table has
Dim TitleT As String 'the title of the table
List1.Clear
Call MakeTables this is a sub not shown here, that does two things: a) puts into a list the following parameters for each Table we want in the report: Number of table;Number of columns in table;Title of table b) populates a variable: TableTxt() with the contents string for each table
Set oWord = CreateObject("word.applica
'first, let us create the header
Set oDoc = oWord.Documents.Add
oWord.selection.TypeText "MY REPORT "
oWord.selection.InsertPara
oWord.selection.InsertAfte
oWord.selection.InsertPara
'oWord.Selection.InsertPar
Set oRange = oWord.ActiveDocument.Range
oRange.Font.Size = 20
Set oRange = oWord.ActiveDocument.Range
oRange.Font.Bold = True
Set oRange = oWord.ActiveDocument.Range
oRange.Font.Name = "arial"
'now let us insert each table into the document
For X = 0 To List1.ListCount - 1
in the following, Splitter is just a function I wrote to split each string in List1 at the semicolons (each string specifies parameters for each table)
NumT = Splitter(";", List1.List(X), 0)
NumColsT = Splitter(";", List1.List(X), 1)
TitleT = Splitter(";", List1.List(X), 2)
p = oDoc.Paragraphs.Count
oDoc.Paragraphs.Add
Set oRange = oDoc.Paragraphs(p + 1).Range
sTemp = TitleT
oRange.Text = sTemp
oRange.Font.Name = "arial"
oRange.Font.Size = 12
oRange.Font.Bold = True
oDoc.Paragraphs.Add
Set oRange = oDoc.Paragraphs(p + 2).Range
in the following, the array TableTxt() got filled in the MakeTables sub. It is the actual data to put in each table
sTemp = TableTxt(X)
oRange.Text = sTemp
oRange.Font.Name = "arial"
oRange.Font.Size = 12
oRange.Font.Bold = False
this converts the appropriate string into a table:
Set oTable = oRange.ConvertToTable(Sepa
:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:=False, _
AutoFit:=True)
For Y = 1 To NumColsT
oTable.Cell(1, Y).Range.Bold = True
Next Y
Next X
oWord.Visible = True
Business Accounts
Answer for Membership
by: mshahzadhaiderPosted on 2008-04-16 at 22:09:22ID: 21374313
DoctorNash,
Please refer to this code or create table this way this will help you alot in working with tables. the way you are using is bit difficult and confusing. i hope you dont mind this. and try to do this way
Thanks,
Dim wdTable as Word.Table
Dim wdDoc as Word.Document
Dim wdRow as Word.Row
Set wdTable = wdDoc.Tables(1)
'To add a Row
set WdRow = wdTable.Rows.Add
'To delete a row
wdTable.Rows(3).Delete