Inserting table in Word and data from Access recordset

J

jesperfj

I need to insert a table in a word document, that I'm opening up from Access
with
late binding.

Currently I simply loop through the Access recordset and create a text
chunk with the fields-to-be seperated by semicolons. (Below "iCount" is the
row count and "iFieldsCount" is the column count).

I then use:

oword.Selection.MoveDown Unit:=WD_Line, Count:=iCount + 1,
Extend:=WD_Extend
oword.Selection.ConvertToTable Separator:=WDSeparateByCommas,
NumColumns:=iFieldsCount, NumRows:=iCount,
AutoFitBehavior:=WD_AutoFitContent

which selects the semicolon-seperated text chunk and converts it to a table.
(oword is my word object).

However this fails sometimes because I don't know the length of the
text chunk and thus don't know how much text to select before
attempting to convert it to a table.
Sometimes I only select part of the text and convert that and text is left
over at the bottom that didn't get converted into the table.

Can I instead create a table and start in cell no. 1 and walk through
that table and simultaniously walk through the recordset and insert
values in the cells?
Can someone get me started with som code to add table, add row, add data to
specifik cell?

Many thanks.
 
J

jesperfj

So far I have:

Dim oDoc As Word.Document 'early binding
Dim oWord As Word.Application 'early binding
Dim myRange As Range
Set myRange = ActiveDocument.Range(start:=140, End:=140)
oWord.Selection.Tables.Add myRange, iCount, iFieldsiCount,
wdWord9TableBehavior, WD_AutoFitFixed

'attempting to insert data
oWord.ActiveDocument.Tables(1).Rows(1).Cells(1)....

What's wrong with the line above?
How do I write text in a cell?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top