Gustav Brock
Gustav at cactus.dk
Sun Jul 10 03:52:03 CDT 2005
Hi Arthur I don't work much with Word - in fact as little as possible - neither with Word tables, but this works for me: Sub AddRows(ByVal lngRowsAdd As Long) Dim tbl As Table Dim lngCount As Long Dim lngRow As Long Set tbl = ActiveDocument.Tables(1) lngRow = tbl.Rows.Count For lngCount = 1 To lngRowsAdd tbl.Rows(lngRow).Range.Rows.Add Next Set tbl = Nothing End Sub /gustav >>> artful at rogers.com 07/09 9:35 pm >>> Thanks for the heads-up Gustav! More specifically, the issue is this. I've got a Word doc (template) that I populate with a bunch of data extracted from a query. I use bookmarks and all that works nicely. The doc (dot) also contains two bookmarks which represent where subtables should appear. (For simplicity think of the query as bank-customer info and the two tables as deposits and withdrawals. That's not the actual case but it suffices as an example.) Because I couldn't make Helen's code run satisfactorily, I have been groping for alternative solutions. But before painting myself into a corner, I'll describe the problem itself... and maybe someone has a more elegant approach. Query1: delivers a bunch of columns from several related tables in one row. I use this to populate the bookmarks in the dot/doc. ST1: (subtable 1) here I need to present the rows returned from a query (deposits, say, sticking to the aforementioned example). ST2: (subtable 2) here I need to present the rows returned from a query (withdrawals, say, again sticking to the aforementioned example). My original concept was to create both tables in the dot file, having a header row and nothing else, then dynamically add as many rows as I need. Let's assume that Query 1 returns 5 rows, and the table has 4 columns. I don't understand how to fill said table with exactly 5 new rows, planting each column into its matching table-column. Since I couldn't figure out how to do that (Helen's code looked promising but I couldn't get even her examples using Northwind to work), I opted instead for placing two bookmarks in the dot file and generating a pair of HTML files, one for each table, then reading the contents of said two files into memory and plonking their data into the two bookmarks representing table 1 and table 2. Perhaps this is a completely asinine way to go about it, but I've never had to populate a table within a Word doc before, so I'm blindly groping around for a way that works. The code that populates the bookmarks is solid. Basically, I create a new Word document object and then assign values to each bookmark from the recordset that I open. All this works splendidly. But now I come to the two sub-tables. What I'm doing at the moment works but doesn't result in exquisite layout... and besides, I'm sure there is a way hipper method of accomplishing the task. During generation of the data, I write the two sub-tables' queries to a pair of HTM files whose name is known. Then I open the Word doc, populate the bookmarks and then deal with the two embedded sub-tables. I use code supplied earlier on this thread to inhale the two HTM files, then bang their contents into the bookmarks Table1 and Table2. It works, sort of. What I would much rather do, if possible, is comprehend how to dynamically expand Table1 to accommodate the X rows that it receives on this call (and the Y rows it might receive on the next call). And ditto with Table2. I.e. Dear <customer given name>, Here is your bank statement for last month. Deposits: Date Amount Withdrawals: Date Amount I could create an additional row for each table and leave it blank, then start populating there and add additional rows as needed. Or each table could consist only of the header row. Or I could add N rows to each table, populate them as I go and delete the unrequired rows. What I don't understand is how to take the N rows returned from Qst 1 and bang each row's values into table T1, column by column, then next row. I have the vague feeling that Range may play a role here, but I don't know where to go with this notion. So let's ignore Q1 (which just goes into the single bookmarks)... no problem there. Now let's say Q2 returns 5 rows and Q3 returns 7 rows. How would I go about populating T1 with the 5 rows returned by Q1? Before writing this, I have invested some time reading the "help"... alas, to no avail. Any advice, guidance or free pain-killers gratefully accepted! Arthur