【问题标题】:Word VBA - insert second table, then start populating itWord VBA - 插入第二个表,然后开始填充它
【发布时间】: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")

【问题讨论】:

    标签: vba ms-access ms-word


    【解决方案1】:

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

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

    2020 年 4 月 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 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
    

    【讨论】:

    • 感谢您的回复。当我更改为该方法时,在 .TypeText oWord.ActiveDocument.Tables.Add Range:=oDoc.Bookmarks("\endofdoc").Range, numRows:=1, numColumns:=5 With oWord.ActiveDocument.Tables Set oRange = .Item(.Count).Range End With ... With oRange.Tables(1) .TypeText "Control Identifier" 的第一个实例上出现错误“找不到方法或数据成员”
    • 然后您需要指定要在其中输入文本的单元格。例如 .Cells.Item(1).Range.Text。指定第一个单元格后,其他相对移动应该没问题。
    • 对不起,伙计,我只是觉得这件事太厚了。 MS 文档说 .moveright 用于我们用With oRange.Tables(1) 覆盖的选择对象,我知道您是说先进入第一个单元格,然后按正常移动。但如果我这样做。 .cells.item(1).Range.Textwith oRange.Tables(1) 下面,我得到方法未找到错误。文档告诉我 .cells 是选择的一部分,但给出了 selection.cells(1) 的示例。
    • with oRange.Tables.Item(1).Range 然后在下一行 .Cells.Item(1).Range
    • 我试了一下,然后我得到了下一行找不到的方法或数据。 (.moveright) ` With oRange.Tables.Item(1).Range .Cells.Item(1).Range.Text = "控件标识符" .MoveRight Unit:=wdCell .TypeText "控件名称"`
    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2017-08-07
    • 2020-08-14
    相关资源
    最近更新 更多