Is there a way to export my query to a pre-existing word document? (1 Viewer)

kmsmith

New member
Local time
Today, 12:49
Joined
Sep 19, 2017
Messages
8
Hey guys!

So currently I have a bit of simple VBA that opens a query in Microsoft word as an RTF:
DoCmd.OutputTo acOutputQuery, "Disclaimed_Report_Excel", _
acFormatRTF, "Disclaimed Well Licenses.rtf", True

My user then has to copy and paste the table into a different word template.

I want to bypass the middle step, sending my query directly from access to the word template.

I have had a lot of success transferring large string from access to word using the following approach:

.FormFields("fldWHEREAS").Result = "" 'Clear anything currently in the form's field
.Bookmarks("fldWHEREAS").Range.Fields(1).Result.Text = Forms![Abandonment Orders]![Text211]
.Visible = True
.Activate

But I am really not sure how/if it could be modified to export a table instead of a string....

Thanks so so much for any insight!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:49
Joined
Feb 28, 2001
Messages
27,332
In Word documents, there is such a thing as a Table (other than "Table of Contents", I mean.) User-defined grid-like tables are members of the "Tables" collection, of which there is one collection per document.

In that table, you have Rows as a collection and Columns as a collection. The Cells are the individual boxes in the table and whether you go by Rows(n) or by Columns(n), you always use Cells(n) for the second collection name. Remember that collections number starting from zero.

So you could make a copy of a template file using the file system object .FileCopy method. Then open the copy using a Word Application object.

The first table in that file is table 0. You can directly address rows and cells within a row (or columns and cells within a column) as described above.

It probably wouldn't hurt for you to do an online search for Word Tables and VBA to see how others have done it. There should be articles in this forum on the subject as well.
 

Users who are viewing this thread

Top Bottom