Export Access Query Results to a Variable-Row Word Table
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.

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

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.

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.




