logo
search
Others

How to Insert Access Query Text into Word Using VBA

John WilsonJohn Wilson Oct 21, 2026 868 views

Question details

The user wants to automate the process of copying query results from Microsoft Access and inserting them into a new Word document using VBA.

How to Insert Access Query Text into Word Using VBA
Product
Microsoft Word
Device & OS
not provided
Scenario
Transferring database query results from Access to a Word document programmatically using VBA macros.
Observed behavior
Manual pasting of query records works perfectly, but the user is unable to automate the clipboard paste operation via code (e.g., currentdocument.select.paste fails).
Before you start

Ensure you have enabled the 'Microsoft Word Object Library' and 'Microsoft ActiveX Data Objects Library' references in your Access VBA editor (Tools > References) to allow cross-application automation.

Solution 1Recommended

Use Word Automation to Populate a Table Directly

Bypass the system clipboard entirely by using an ADO Recordset to loop through query results and directly populate cells in a Word document table. This prevents clipboard access errors.

Relying on the system clipboard in VBA can cause runtime errors if the clipboard is locked by another process or if the code executes faster than the OS can handle. Directly assigning values from a recordset to Word table cells is a much more robust approach.

1
Open the VBA Editor

In Microsoft Access, press ALT + F11 to open the Visual Basic for Applications (VBA) editor.

2
Set up Word Automation

Create an instance of the Word Application and a new Document object in your code (e.g., Dim objWord As Object, Set objWord = CreateObject("Word.Application")).

3
Loop through the ADO Recordset

Open your query as an ADO recordset. Use a Do Until rst.EOF loop to iterate through the records.

4
Assign values to table cells

Within the loop, add a new row to your Word table using .Rows.Add, and assign recordset fields to specific cells using .Rows.Last.Cells(1).Range.Text = rst!FieldName.

Use Word Automation to Populate a Table Directly
Performance Tip: If your query contains thousands of records, hiding the Word application during the loop (objWord.Visible = False) can significantly speed up the execution time.
Free Microsoft Office alternative

Looking for a Lightweight Office Suite?

While Microsoft Access is a specialized database tool, for your everyday document processing, spreadsheets, and presentations, WPS Office offers a highly compatible, lightweight, and free alternative to Microsoft Office.

  1. 1. Download the Installer: Visit the official WPS website and download the free WPS Office installer for your operating system.
  2. 2. Install WPS Office: Run the downloaded file and follow the on-screen prompts to complete the installation in just a few minutes.
  3. 3. Open Your Documents: Launch WPS Writer to instantly open, edit, and save your existing Word documents with excellent formatting retention.
Free and lightweight office suite for Windows, Mac, and LinuxHigh format compatibility with Microsoft Word documents (.docx)Supports advanced macros and VBA in the Professional versionFamiliar user interface for seamless migration and quick learning
QA img-9

Frequently Asked Questions

Why does clipboard pasting fail in my VBA code?

Pasting from the clipboard in VBA often fails because the code executes faster than the operating system's clipboard can process the data, or because security settings and background applications are temporarily locking clipboard access. Bypassing the clipboard using direct automation prevents these timing and access errors.

How do I enable Word automation in Access VBA?

To enable Word automation, open the VBA Editor in Access, click on 'Tools' in the top menu, select 'References', scroll down the list, and check the box next to 'Microsoft Word Object Library'. Click OK to apply.

What is the GetString method in an ADO Recordset?

The GetString method is a built-in ADO function that quickly exports an entire recordset into a single string variable. It allows you to specify custom delimiters for columns and rows, making it highly efficient for transferring bulk data into text formats or Word documents without looping through individual records.