View Single Post
  #3   Report Post  
Posted to microsoft.public.word.tables
Eric[_2_] Eric[_2_] is offline
external usenet poster
 
Posts: 8
Default populate a word table programatically

Hi Jay

The code will be in an Excel wbk (or addin) that the user will have open, so
I'm not worried about excel not being there. I don't have too much data to
transfer either, so I'm not concerned with speed right now. See below for
technique I would typically use to transfer data around.

The part I'm more concerned about is the mechanics of populating the Word
table, and am thinking it should go something like this
1) create a preformatted table in my protected Word do, with only a header
2) for each row of data
2.1) add a new row to the table (not sure how to do this)
2.2) paste the row into the table (not sure how to do this)
2.3) apply any needed formatting, i.e. currency $ and commas (not sure
how to do this)

Here's how I deal with moving Excel data around
1) create a User Defined Type to act as a data container for each row
2) create the table in code using three Named Ranges ("ptrTableStartCell",
"ptrTableEndCell", "ptrLastTableColumn")
3) for each rngRow in myTable.Rows
with udtMyData
.FirstName = rngRow.Cells(1, 1)
.FirstName = rngRow.Cells(1, 2)
end with

Any critique, pointers much appreciated. I like your DAO idea, but what I'd
really like to do eventually is use a 2008 VSTO based solution, either with
ADO, XML or LINQ. This would facilitate testing, reuse, and security for
both myself and the user. Unfortunately I haven't the slightest idea how to
do that now :-)

Thanks,
Eirc


"Jay Freedman" wrote in message
...
Eric wrote:
Any links or tips to do this most appreciated. The data would
currently come from Excel, and get there by VBA. The word doc is
protected with form filling only allowed. v2003 on both.

I posted this on office development as well, as I hadn't seen this
forum first. If there is a more appropriate forum please let me know.

TIA,
Eric


The essential idea for retrieving recordsets (rows) from Excel is shown in
http://www.word.mvps.org/FAQs/InterD...ordWithDAO.htm.

In place of the MsgBox statement in that code, you would need something
like this (assuming the last name is in column A of the worksheet and the
first name is in column B):

With ActiveDocument.FormFields
.Item("LastName").Result = rs.Fields(0).Value
.Item("FirstName").Result = rs.Fields(1).Value
End With

Of course, iterating the rows with While Not rs.EOF doesn't make much
sense for a single Word form unless the worksheet has only one row.

If you get stuck, post back.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.