Word VBA-插入第二个表,然后开始填充它

问题描述 投票:0回答:1

在我的Access VBA中,以下创建并填充Word表没有问题。

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
    ......

以此类推,当表完成时,我插入一个分页符和一些文本,然后插入第二个表。

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
  ......

创建第二个表,但其中没有任何填充。如果我在第二张表创建后放置oWord.Selection.Style = "Table Grid"-它会在第一张表周围创建一个网格,但不会在第一张表中添加其他文本或行。

[我知道我可以进行set oTable =类型的表创建,但是当我尝试选择它时,它似乎是从Word.Document而不是Word.Application中选择,而我随后的.Selection不起作用我的动态创作适合.moveright。

有什么想法我要去哪里?

access-vba word-vba
1个回答
0
投票

这是因为您没有选择要使用的特定表。对于您的第一个表和后续表,请尝试

Dim myRange as Word.range
with oWord.ActiveDocument.Tables
    set myRange=.Item(.Count).Range
end with

with myRange.Tables(1)

etc

更新2020 / Apr / 30

我希望以下更新更清楚。

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 myTable to the range of 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
© www.soinside.com 2019 - 2024. All rights reserved.