Is there syntax to output information to Word in such a way as it will go into a table? My client wants the info to be in a grid with lines. I don’t have any problems outputting to Word using the Word object, I just want to get a handle on what is involved to put it in table form.
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Output information to a Word table?
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Output information to a Word table?
- This topic has 15 replies, 4 voices, and was last updated 15 years, 8 months ago.
Viewing 2 reply threadsAuthorReplies-
WBell
AskWoody_MVPAugust 4, 2009 at 2:19 pm #1172277Is there syntax to output information to Word in such a way as it will go into a table? My client wants the info to be in a grid with lines. I don’t have any problems outputting to Word using the Word object, I just want to get a handle on what is involved to put it in table form.
It is possible to do, but the automation code to do it is quite complex as you have to construct the table in VBA and then keep track of where you are with indexes and the like. If I were doing it, I would probably export the data to Excel and then paste the Excel cells into Word as a table. Neither approach is trivial, but it is definitely doable.
-
WSHansV
AskWoody LoungerAugust 4, 2009 at 2:41 pm #1172280Here is an example. It uses the following variables:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim wrdTbl As Word.Table
Dim c As Integer
Dim n As Integer
Dim r As Integer
Dim rst As DAO.RecordsetI will assume that wrdApp and wrdDoc have already been set, and that the recordset rst has already been opened. The following code creates a table in the document with as many columns as there are fields in the recordset. It fills the first row with the field names, then adds a row for each record.
Code:n = rst.Fields.Count ' Create table Set wrdTbl = wrdDoc.Tables.Add(Range:=wrdApp.Selection.Range, NumRows:=1, NumColumns:=n) r = 1 ' Fill the first row with field names For c = 1 To n wrdTbl.Cell(r, c).Range.Text = rst.Fields(c - 1).Name Next c ' Loop through the records Do While Not rst.EOF r = r + 1 ' Add a row wrdTbl.Rows.Add ' Fill from record For c = 1 To n wrdTbl.Cell(r, c).Range.Text = rst.Fields(c - 1).Value Next c rst.MoveNext Loop
Followed by the usual cleanup (close the recordset etc.)
-
WSPeterN
AskWoody Lounger -
WSPeterN
AskWoody LoungerAugust 8, 2009 at 9:29 pm #1172766Here is an example. It uses the following variables:
Dim wrdApp As Word.Document
Dim wrdDoc As Word.Document
Dim wrdTbl As Word.Table
Dim c As Integer
Dim n As Integer
Dim r As Integer
Dim rst As DAO.RecordsetHans, I was just looking at this. In your declarations your first one is wrdApp as Word.Document. Are both wrdApp and wrdDoc documents or should wrdApp be Word.Application? It certainly makes more sense to me given your naming of the variable.
-
WSHansV
AskWoody LoungerAugust 9, 2009 at 4:26 am #1172772Hans, I was just looking at this. In your declarations your first one is wrdApp as Word.Document. Are both wrdApp and wrdDoc documents or should wrdApp be Word.Application? It certainly makes more sense to me given your naming of the variable.
You are entirely correct. It should be
Dim wrdApp As Word.Application
I have corrected my reply. Thanks for pointing out my mistake!
-
WSPeterN
AskWoody LoungerAugust 13, 2009 at 3:07 pm #1173408OK. I have got this far.
The next thing I am trying to do is open a template and set the insertion point at a specific spot – bkmk1 (bookmark1) and then insert the table there.
In fact, since I already know what the column headings will be, just not how many rows there will be, what I would ideally like to do is have a table set up in the template with the column headings already there and formatted then start adding the data into the second row (either create the second row and start adding from there or start at a blank second row and add in additional ones as needed). I assume this would only mean dumping the three lines of code that insert the field names.
The problem I have with the current code is that it doesn’t seem to actually go to the bookmark, or if it does, it then goes back to the beginning and inserts the table there. I am very limited in my understanding of the Word Object and ranges so I can only assume that
Set wrdTbl = wrdDoc.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:=n)
is going back to the beginning.
Code:Option Compare Database Option Explicit Private Const m_strDIR As String = "C:Documents and SettingsPNMy DocumentsDataDump" Public Sub WordClin() Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Dim wrdTbl As Word.Table Dim c As Integer Dim n As Integer Dim r As Integer Dim rst As DAO.Recordset Dim qdf As DAO.QueryDef Dim dbs As DAO.Database Dim dteStart As Date Dim dteEnd As Date Set wrdApp = New Word.Application wrdApp.Documents.Open (m_strDIR & "NIS - Template.DOC") Set wrdDoc = Word.ActiveDocument dteStart = #4/1/2009# dteEnd = #6/30/2009# Set dbs = CurrentDb() Set qdf = dbs.QueryDefs("aaa") qdf.Parameters("[forms]![frmISAPDates]![txtStartDate]") = dteStart qdf.Parameters("[forms]![frmISAPDates]![txtEndDate]") = dteEnd Set rst = qdf.OpenRecordset(dbOpenDynaset) n = rst.Fields.Count wrdDoc.GoTo what:=wdGoToBookmark, Name:="Bkmk1" ' Create table Set wrdTbl = wrdDoc.Tables.Add(Range:=wrdApp.Selection.Range, NumRows:=1, NumColumns:=n) r = 1 ' Fill the first row with field names For c = 1 To n wrdTbl.Cell(r, c).Range.Text = rst.Fields(c - 1).Name Next c ' Loop through the records Do While Not rst.EOF r = r + 1 ' Add a row wrdTbl.Rows.Add ' Fill from record For c = 1 To n wrdTbl.Cell(r, c).Range.Text = Nz(rst.Fields(c - 1).Value, 0) Next c rst.MoveNext Loop wrdDoc.SaveAs FileName:=m_strDIR & _ "NIS - " & FormatDateTime(Date, vbLongDate) & ".DOC" wrdDoc.Close wrdApp.Quit ' clean up Set wrdDoc = Nothing Set wrdApp = Nothing rst.Close Set rst = Nothing End Sub
-
WSPeterN
AskWoody LoungerAugust 13, 2009 at 3:20 pm #1173414Never mind about the range question, I changed this
Set wrdTbl = wrdDoc.Tables.Add(Range:=wrdApp.Selection.Range, NumRows:=1, NumColumns:=n)
to this
Set wrdTbl = wrdDoc.Tables.Add(Range:=ActiveDocument.Bookmarks(1).Range, NumRows:=1, NumColumns:=n)
Nuthin I hate worse than being defeated by VBA and obscure, circular help files
However, I still would like to move on to the next bit which is adding data into a pre-existing table with headers.
I assume now that this means the above solution will be redundant since I won’t need to create the table but rather go to a specific range within an existing one
-
WSHansV
AskWoody Lounger -
WSPeterN
AskWoody LoungerAugust 13, 2009 at 6:54 pm #1173450 -
WSHansV
AskWoody Lounger -
WSPeterN
AskWoody LoungerAugust 13, 2009 at 10:07 pm #1173460An instance of Word created by Automation is hidden by default. You can make it visible by inserting a line
wrdApp.Visible = True
at the point where you want it (after creating the instance, of course)
Okay. As usual, a simple solution if you know where to look.
Last item for this test phase which may or may not be a problem for implementation.
Right now, I am running the sub from the Immediate window for testing. The first time I run it, it runs fine and the instance of Winword.exe is not showing up in the task manager after the sub is done. When I go and run it the second time, I get runtime error 642: The remote server machine does not exist or is unavailable. When I click on the debug button, it is hanging on the line Set wrdDoc = Word.ActiveDocument. If I end the sub, go to the task manager and manually kill winword.exe and run it the third time, then it is fine again. This process also leaves a word temp file in the folder where my template is.
So the question: Is this just something annoying to live with in the development phase or is this going to be a problem for implementation? Is there a reason why it is only happening every other time I run the sub?
-
WSHansV
AskWoody LoungerAugust 14, 2009 at 3:01 am #1173472When using Automation to control Word, EVERY Word object that you use must refer directly or indirectly to the Word.Application object that you create, in your code wrdApp, otherwise you’ll create an extra instance of Word that is not controlled by your application and that will remain in memory. In the line
Set wrdDoc = Word.ActiveDocument
Word is a generic object, not your own wrdApp object. You should change it to
Set wrdDoc = wrdApp.ActiveDocument
Or even better, combine the two lines
wrdApp.Documents.Open (m_strDIR & “NIS – Template.DOC”)
Set wrdDoc = Word.ActiveDocumentto
Set wrdDoc = wrdApp.Documents.Open (m_strDIR & “NIS – Template.DOC”)
-
WSPeterN
AskWoody LoungerAugust 14, 2009 at 6:20 am #1173479When using Automation to control Word, EVERY Word object that you use must refer directly or indirectly to the Word.Application object that you create, in your code wrdApp, otherwise you’ll create an extra instance of Word that is not controlled by your application and that will remain in memory.
Thanks so much Hans. that got it. a three beer effort!
-
-
-
-
WSjeremybarker
AskWoody LoungerViewing 2 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Remove a User from Login screen
by
CWBillow
1 hour, 37 minutes ago -
TikTok fined €530 million for sending European user data to China
by
Nibbled To Death By Ducks
2 hours, 46 minutes ago -
Microsoft Speech Recognition Service Error Code 1002
by
stanhutchings
8 hours, 50 minutes ago -
Is it a bug or is it expected?
by
Susan Bradley
7 hours, 45 minutes ago -
Image for Windows TBwinRE image not enough space on target location
by
bobolink
5 hours, 23 minutes ago -
Start menu jump lists for some apps might not work as expected on Windows 10
by
Susan Bradley
13 hours, 23 minutes ago -
Malicious Go Modules disk-wiping malware
by
Alex5723
3 hours ago -
Multiple Partitions?
by
CWBillow
3 hours, 39 minutes ago -
World Passkey Day 2025
by
Alex5723
20 hours, 59 minutes ago -
Add serial device in Windows 11
by
Theodore Dawson
1 day, 12 hours ago -
Windows 11 users reportedly losing data due forced BitLocker encryption
by
Alex5723
7 minutes ago -
Cached credentials is not a new bug
by
Susan Bradley
1 day, 16 hours ago -
Win11 24H4 Slow!
by
Bob Bible
1 day, 16 hours ago -
Microsoft hiking XBox prices starting today due to Trump’s tariffs
by
Alex5723
1 day, 14 hours ago -
Asus adds “movement sensor” to their Graphics cards
by
n0ads
1 day, 19 hours ago -
‘Minority Report’ coming to NYC
by
Alex5723
1 day, 15 hours ago -
Apple notifies new victims of spyware attacks across the world
by
Alex5723
2 days, 3 hours ago -
Tracking content block list GONE in Firefox 138
by
Bob99
2 days, 3 hours ago -
How do I migrate Password Managers
by
Rush2112
1 day, 11 hours ago -
Orb : how fast is my Internet connection
by
Alex5723
1 day, 12 hours ago -
Solid color background slows Windows 7 login
by
Alex5723
2 days, 15 hours ago -
Windows 11, version 24H2 might not download via Windows Server Updates Services
by
Alex5723
2 days, 14 hours ago -
Security fixes for Firefox
by
Susan Bradley
1 day, 14 hours ago -
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
3 days, 2 hours ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
3 days, 11 hours ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
3 days, 2 hours ago -
Return of the brain dead FF sidebar
by
EricB
2 days, 13 hours ago -
Windows Settings Managed by your Organization
by
WSDavidO61
1 day, 16 hours ago -
Securing Laptop for Trustee Administrattor
by
PeachesP
18 hours, 30 minutes ago -
The local account tax
by
Susan Bradley
2 days, 14 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.