VBA & Macro Problems

Export Access Query Results to a Variable-Row Word Table

Elise WilliamsElise Williams Nov 20, 2026 869 views

Question details

Create a Word letter from an Access form and populate a table with a variable number of query results for the selected person.

Export Access Query Results to a Variable-Row Word Table
Product
Microsoft Access and Microsoft Word
Device & OS
not provided
Scenario
A form button should generate a letter from a Word template and list failed questions or review items associated with a person.
Observed behavior
The desired letter contains one table row for each matching query result.
Before you start

This approach requires desktop versions of Access and Word on Windows, a saved Word template, and an Access query that returns the selected person's review items. Prepare a table in the template with a header row and no pre-existing data rows.

Solution 1Recommended

Use an Access form button to fill a Word template table

Use this approach to create a letter for the current form record and add one Word table row per matching query result.

The example uses late binding for Word, so it does not require a Word object library reference. Replace the example control, query field, parameter, and template path with the names used in your database.

1
Prepare the query

Open qryFileReview in Access Query Design and ensure it returns a ReviewItem field. Add a Long Integer parameter named pPersonID and use [pPersonID] as the criterion for the person's ID field. Rename these examples in the VBA if your query uses different names.

2
Prepare the Word template

In Word, create or open the letter template and insert a table with a header row. The code fills column 1 of the first table; save the template as a .dotx file and note its full path.

3
Add the form button

Open the Access form in Design View, add a command button named cmdGenerateLetter, and set its On Click property to [Event Procedure]. Click the ellipsis button to open the VBA editor, then use the following procedure. Change the template path and names to match your setup: Private Sub cmdGenerateLetter_Click() Dim qdf As Object, rs As Object Dim wdApp As Object, wdDoc As Object, tbl As Object Dim msg As String On Error GoTo ErrHandler If IsNull(Me!PersonID) Then MsgBox "Select a person first." Exit Sub End If Set qdf = CurrentDb.QueryDefs("qryFileReview") qdf.Parameters("pPersonID") = Me!PersonID Set rs = qdf.OpenRecordset() Set wdApp = CreateObject("Word.Application") Set wdDoc = wdApp.Documents.Add("C:\Templates\ReviewLetter.dotx") Set tbl = wdDoc.Tables(1) Do Until rs.EOF tbl.Rows.Add tbl.Cell(tbl.Rows.Count, 1).Range.Text = CStr(Nz(rs.Fields("ReviewItem").Value, "")) rs.MoveNext Loop rs.Close wdApp.Visible = True Exit Sub ErrHandler: msg = Err.Description On Error Resume Next If Not rs Is Nothing Then rs.Close If Not wdDoc Is Nothing Then wdDoc.Close False If Not wdApp Is Nothing Then wdApp.Quit MsgBox "Could not generate the letter: " & msg End Sub

4
Generate and save the letter

Open the form in Form View, select a person, and click the new button. Check the table in the Word document, then use File > Save As to save the letter.

Use an Access form button to fill a Word template table
No matching results: If the query returns no records, the letter opens with only the table's header row. To fill a different table or column, change Tables(1) or the column number in tbl.Cell.
Free Microsoft Office alternative

Edit Word-Compatible Letters with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for editing Word-compatible letters. Open existing documents in WPS Writer for an easy transition; the Access-to-Word VBA automation itself still requires Microsoft Access and Word.

Review and edit letter tables in WPS WriterOpen and save Microsoft Word DOCX documentsUse a familiar interface for everyday document editingWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How does the button select results for the current person?

The procedure reads the form's PersonID control and assigns its value to the query parameter pPersonID. The query must use that parameter as a criterion on the person ID field.

How can I fill a different table or column in the template?

Change Tables(1) to the position of the target table in the Word document. Change the second argument in tbl.Cell(tbl.Rows.Count, 1) to the target column number.

What happens if the query returns no review items?

The loop adds no rows, so the new Word document opens with the template's header row. Check the query criteria and selected person if results were expected.

Do I need to add a Word reference in the VBA editor?

Not for the sample code. It uses CreateObject("Word.Application") and Object variables for late binding, but desktop Word must still be installed.