Sizing MS Word table to same size as recordset (1 Viewer)

cnstarz

Registered User.
Local time
Today, 10:22
Joined
Mar 7, 2013
Messages
89
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:

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

vbaInet

AWF VIP
Local time
Today, 16:22
Joined
Jan 22, 2010
Messages
26,374
This is more of a Word VBA question and you'll get better responses if you post it in the right forum.

I've hardly ever needed to use Word VBA but I can see that you're not using the Word instance, i.e. wDoc, with the Selection object.
 

cnstarz

Registered User.
Local time
Today, 10:22
Joined
Mar 7, 2013
Messages
89
Yikes, I thought I had posted in the Word forum! My bad! Well, maybe someone might be fluent with Word VBA here, too.Since this is being run from Access VBA someone here might know something too.

Adding changing "Selection.InsertRowsBelow 5" to "wDoc.Selection.InsertRowsBelow 5" doesn't do anything either. :(
 

vbaInet

AWF VIP
Local time
Today, 16:22
Joined
Jan 22, 2010
Messages
26,374
VBA as a language is the same across all applications the difference is the object model. I'll request for it to be moved.

By the way, shouldn't you be selecting a row instead of the table? I would imagine this is what you would do before attempting to insert below rows.
 

vbaInet

AWF VIP
Local time
Today, 16:22
Joined
Jan 22, 2010
Messages
26,374
By the way, shouldn't you be selecting a row instead of the table? I would imagine this is what you would do before attempting to insert below rows.
In relation to this point, I don't think you should be selecting anything. There should be a way of just inserting below a certain range without needing to select it.
 

cnstarz

Registered User.
Local time
Today, 10:22
Joined
Mar 7, 2013
Messages
89
edit: crap i accidentally derped this post...
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 16:22
Joined
Jan 22, 2010
Messages
26,374
No, I don't think it's necessary. Selecting slows things down.

I'll fire up some code in a moments.
 

cnstarz

Registered User.
Local time
Today, 10:22
Joined
Mar 7, 2013
Messages
89
Figured it out after much trial and error!

Code:
    Dim wDoc as Word.Document
    Dim wTable As Word.Table
    Dim wCell As Word.Cell
    [COLOR=Blue]Dim wRow As Word.Row[/COLOR]

    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
[COLOR=Blue]        Set wRow = wTable.Rows.Last
        Do While wRow.Index < rs.RecordCount
            Set wRow = wTable.Rows.Last
            wTable.Rows.Add
        Loop[/COLOR]
    End If

Now I need to figure out how to populate that word table with records from my query...
 

vbaInet

AWF VIP
Local time
Today, 16:22
Joined
Jan 22, 2010
Messages
26,374
Exactly my point. Good job!

You use the Cell object to populate the table.

And I think you're going about it the wrong way. You should dimension the table when it's created.
 

Users who are viewing this thread

Top Bottom