【发布时间】:2020-08-14 20:02:34
【问题描述】:
在我的 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 行事。
有什么想法会出错吗?
---更新后工作--- 非常感谢@freeflow 的耐心和帮助。他的代码很棒,我只需要对这部分进行微调。 ... 移动单元 wdRow 工作,但没有额外的行退出表。我在为该行的第二列引用 .Cells(2) 时也遇到了问题 - 但先移动然后输入文本就可以了。
这个。
myRow.Move unit:=wdRow, Count:=1
myRow.Cells(1).Range.Text = rs("Control_Main")
myRow.Cells(2).Range.Text = rs("Title")
变成了这个。
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")
【问题讨论】: