W2002 can't keep up

M

Martijn

Hi.

I'm executing a macro which inserts data from my database
into a table in Word. I start with a one-cell-table and
then every new row from the db should be a new line in the
table, therefore I am using a For...Next routine like this:

For rowcounter=0 to NumRows

[insert statements]

ActiveDocument.Tables(1).Range.Rows.Add
Selection.MoveDown Unit:=wdLine, Count:=1

Next

The problem seems to be that Word cannot keep up with the
pace of this for...next loop. Particularly when it comes
to moving down to the next row. Without warning, it
randomly skips moving to the new row, thus leaving me with
cells with more than one row's worth of information. I
can't find any logic in why the move is not performed.

Is there a way to slow down the execution of the code so
that Word does keep up with it? Changing the unit
to 'wdParagraph' did not do the trick.

Thanks for thinking along!
 
J

Jay Freedman

Hi Martijn,

The largest part of the problem is that you're manipulating the insertion
point with the Selection.MoveDown (and presumably in other parts of the code
with Selection.TypeText or something similar). This forces Word to redraw
the screen, which is computationally expensive and especially so within
tables. You could speed things up a bit by using a Range object instead of
the Selection.

However, there's a much better solution. Don't try to create the table one
row at a time and place things in it. Instead, construct all the text of the
table from the database fields as plain text, with the contents of the cells
of each row separated by tab characters and each row ending with a paragraph
mark. When it's all in place, point a Range object or the Selection at the
entire chunk of text and use the .ConvertTextToTable method.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Martijn > écrivait :
In this message said:
Hi.

I'm executing a macro which inserts data from my database
into a table in Word. I start with a one-cell-table and
then every new row from the db should be a new line in the
table, therefore I am using a For...Next routine like this:

For rowcounter=0 to NumRows

[insert statements]

ActiveDocument.Tables(1).Range.Rows.Add
Selection.MoveDown Unit:=wdLine, Count:=1

Next

The problem seems to be that Word cannot keep up with the
pace of this for...next loop. Particularly when it comes
to moving down to the next row. Without warning, it
randomly skips moving to the new row, thus leaving me with
cells with more than one row's worth of information. I
can't find any logic in why the move is not performed.

Is there a way to slow down the execution of the code so
that Word does keep up with it? Changing the unit
to 'wdParagraph' did not do the trick.

Have you tried something like this:

'_______________________________________
Dim MyRow As Row

Set MyRow = ActiveDocument.Tables(1).Rows.Add

With MyRow
.Cells(1).Range.Text = "Name"
.Cells(2).Range.Text = "Company"
.Cells(3).Range.Text = "City"
'etc.
End With
'_______________________________________

It avoids the selection object, which is not very efficient as you have
discovered.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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