Reputation: 704
in my Access VBA I have the following creating and populating a Word Table without issue.
oWord.ActiveDocument.Tables.Add Range:=oDoc.Bookmarks("\endofdoc").Range, NumRows:=1, NumColumns:=5
rs.MoveFirst
r = 0 'current row counter
'start the recordset loop reserving the top two rows for header type stuff.
With oWord.Selection
.TypeText "Control Identifier"
.MoveRight Unit:=wdCell
.TypeText "Control Name"
.MoveRight Unit:=wdCell
.MoveRight Unit:=wdCell
.MoveRight Unit:=wdCell
'first data row
.MoveRight Unit:=wdCell
.TypeText rs("Control_Main")
.MoveRight Unit:=wdCell
.TypeText rs("Title")
.MoveRight Unit:=wdCell
.MoveRight Unit:=wdCell
.MoveRight Unit:=wdCell
......
and so on when the table is completed I insert a page break and some text, then I insert a second table.
oWord.ActiveDocument.Tables.Add Range:=oDoc.Bookmarks("\endofdoc").Range, NumRows:=1, NumColumns:=3
rs.MoveFirst
With oWord.Selection
'first data row
.MoveRight Unit:=wdCell
.TypeText rs("Control_main")
.MoveRight Unit:=wdCell
.TypeText rs("Title")
.MoveRight Unit:=wdCell
......
The second table gets created but nothing populates in it. If I put oWord.Selection.Style = "Table Grid"
after my second table creation - it creates a grid around the first table but no additional text or rows are added to the first table.
I know I can do a set oTable =
type of table creation, but then when I try and select it, it seems to select from the Word.Document instead of the Word.Application and my subsequent with .Selection doesn't act right for .moveright for my dynamic creation.
Any ideas where i'm going awry?
---Update post working --- A very special thank you to @freeflow for his patience and assistance. His code was great, I just needed to do a slight tweak mainly with this part. ... The move unit wdRow worked, but without the additional row it exited the table. I also had problems with referring to .Cells(2) for 2nd column in that row - but moving first then entering text worked just fine.
this.
myRow.Move unit:=wdRow, Count:=1
myRow.Cells(1).Range.Text = rs("Control_Main")
myRow.Cells(2).Range.Text = rs("Title")
became this.
oDoc.Tables(1).Rows.Add
myRow.Move Unit:=wdRow, Count:=1
myRow.Cells(1).Range.Text = rs("Control_main")
myRow.Move Unit:=wdCell, Count:=1
myRow.Cells(1).Range.Text = rs("Title")
Upvotes: 0
Views: 468
Reputation: 4355
Its because you are not selecting the specific table on which you wish to work. For your first and subsequent tables try
Dim myRange as Word.range
with oWord.ActiveDocument.Tables
set myRange=.Item(.Count).Range
end with
with myRange.Tables(1)
etc
Update 2020/Apr/30
I hope the following update is clearer to follow.
rs.MoveFirst
r = 0 'current row counter
'start the recordset loop reserving the top two rows for header type stuff.
'Add the first table at the end of the document
oDoc.Tables.Add Range:=oDoc.Bookmarks("\endofdoc").Range, NumRows:=1, NumColumns:=5
'set myRow to the range of the first row in the last table in the document
Dim myRow As Word.Range
' .First is sugar for .Item(1)
Set myRow = oDoc.Tables.Item(oDoc.Tables.Count).Range.Rows.First.Range
myRow.Cells(1).Range.Text = "Control Identifier"
myRow.Cells(2).Range.Text = "Control Name"
myRow.Move unit:=wdRow, Count:=1
'first data row - its not clear from your code if you are entering data into the first column or not
' so the 1 and 2 below may need adjusting
' such is the difficulties introduced by emulating keyboard movements.
myRow.Cells(1).Range.Text = rs("Control_Main")
myRow.Cells(2).Range.Text = rs("Title")
etc
'add the next table at the end of the document
oDoc.Tables.Add Range:=oDoc.Bookmarks("\endofdoc").Range, NumRows:=1, NumColumns:=5
'set myRow to the range of the first row in the last table in the document
Dim myRow As Word.Range
' .First is sugar for .Item(1)
Set myRow = oDoc.Tables.Item(oDoc.Tables.Count).Range.Rows.First.Range
myRow.Cells(1).Range.Text = "Control Identifier"
myRow.Cells(2).Range.Text = "Control Name"
myRow.Move unit:=wdRow, Count:=1
'first data row - its not clear from your code if you are entering data into the first column or not
' so the 1 and 2 below may need adjusting
' such is the difficulties introduced by emulating keyboard movements.
myRow.Cells(1).Range.Text = rs("Control_Main")
myRow.Cells(2).Range.Text = rs("Title")
etc
Upvotes: 1