My end goal is to populate a pre-existing table in an MS Word document with records from a query. The easiest way I've found (through scouring the internet) is to start with the code below (ran during OnClick() even in Access) to get the table the same size as the recordset:
The code will shrink the table down just fine if the table has more rows than the recordset +1 (for header column). My hangup with this is the last line ("Selection.InsertRowsBelow 5") isn't executing; rows are not being added to the table. I get no errors -- it just does nothing. I set it as "Selection.InsertRowsBelow 5" arbitrarily just to see if it would even add rows, and sure enough it's not. Any advice folks?
Code:
Dim wDoc As Word.Document
Dim wTable As Word.Table
Dim wCell As Word.Cell
Set wDoc = appWord.Documents.Add(strDocLoc)
wDoc.Visible = True
Set wTable = wDoc.Tables(1)
If wTable.Rows.Count > rs.RecordCount Then
For Each wCell In wTable.Range.Cells
If wCell.RowIndex > rs.RecordCount + 1 Then
wTable.Rows(wCell.RowIndex).Delete
End If
Next wCell
ElseIf wCell.RowIndex < rs.RecordCount + 1 Then
wDoc.Tables(1).Select
Selection.InsertRowsBelow 5
End If