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 Lounger
Viewing 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
-
AugLoop.All (TEST Augmentation Loop MSIT)
by
LarryK
3 hours, 30 minutes ago -
Boot Sequence for Dell Optiplex 7070 Tower
by
Serge Carniol
3 hours, 19 minutes ago -
OTT Upgrade Windows 11 to 24H2 on Unsupported Hardware
by
bbearren
6 hours, 52 minutes ago -
Inetpub can be tricked
by
Susan Bradley
8 hours, 11 minutes ago -
How merge Outlook 2016 .pst file w/into newly created Outlook 2024 install .pst?
by
Tex265
4 hours, 57 minutes ago -
FBI 2024 Internet Crime Report
by
Alex5723
10 hours, 41 minutes ago -
Perplexity CEO says its browser will track everything users do online
by
Alex5723
8 hours, 6 minutes ago -
Login issues with Windows Hello
by
CWBillow
21 hours, 47 minutes ago -
How to get into a manual setup screen in 2024 Outlook classic?
by
Tex265
9 hours, 41 minutes ago -
Linux : ARMO rootkit “Curing”
by
Alex5723
1 day, 9 hours ago -
Employee monitoring app leaks 21 million screenshots in real time
by
Alex5723
1 day, 9 hours ago -
Google AI is now hallucinating idioms
by
Alex5723
1 day, 9 hours ago -
april update
by
69800
12 hours, 9 minutes ago -
Windows 11 Insider Preview build 27842 released to Canary
by
joep517
1 day, 10 hours ago -
Quick Fix for Slowing File Explorer
by
Drcard:))
1 day, 11 hours ago -
WuMgr not loading?
by
LHiggins
6 hours, 49 minutes ago -
Word crashes when accessing Help
by
CWBillow
15 hours, 9 minutes ago -
New Microsoft Nag — Danger! Danger! sign-in to your Microsoft Account
by
EricB
1 day, 10 hours ago -
Blank Inetpub folder
by
Susan Bradley
1 day, 8 hours ago -
Google : Extended Repair Program for Pixel 7a
by
Alex5723
1 day, 21 hours ago -
Updates seem to have broken Microsoft Edge
by
rebop2020
1 day, 7 hours ago -
Wait command?
by
CWBillow
1 day, 14 hours ago -
Malwarebytes 5 Free version manual platform updates
by
Bob99
2 days, 3 hours ago -
inetpub : Microsoft’s patch for CVE-2025–21204 introduces vulnerability
by
Alex5723
2 days, 10 hours ago -
Windows 10 finally gets fix
by
Susan Bradley
2 days, 19 hours ago -
AMD Ryzen™ Chipset Driver Release Notes 7.04.09.545
by
Alex5723
2 days, 20 hours ago -
How to use Skype after May?
by
Joann
1 day, 5 hours ago -
Win 7 MS Essentials suddenly not showing number of items scanned.
by
Oldtimer
2 days, 15 hours ago -
France : A law requiring messaging apps to implement a backdoor ..
by
Alex5723
3 days, 9 hours ago -
Dev runs Windows 11 ARM on an iPad Air M2
by
Alex5723
3 days, 10 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.