Speeding Up Word Table

A

ACFalcon

I have a fairly large Word document that a .NET page creates......between 2
and 10 MB depending on what the user selects. The problem is that the file
works great if I am just inserting text, but if I try to build a table in
word all of a sudden the file takes sometimes up to 30 minutes (100% CPU
Usage) to be created. I cut out the code to create the tables and everything
runs fairly quickly (less than 5 minutes). Here is a sample of the table
creation code. I know my .NET datatables are not the issue, because I can
load and read them fine in a matter of seconds.

oWT = oDoc.Sections(a).Range.Tables.Add(oDoc.Sections(a).Range,
oDT.Rows.Count + 1, 3)
With oWT.Rows(1)
.Cells(1).Delete()
.Cells(1).Range.Text = "DATABASE: " &
oParent.Attributes("Database").Value
.Cells(1).WordWrap = True
.Cells(1).Range.Font.Bold = True
.Cells(1).Width = 300
.Cells(2).Range.Text = "TABLE: " &
oParent.Attributes("Name").Value
.Cells(2).WordWrap = True
.Cells(2).Range.Font.Bold = True
.Cells(2).Width = 300
End With
c = 2
For Each oDRW In oDT.Rows
With oWT.Rows(c)
.Cells(1).Delete()
.Cells(1).Delete()
.Cells(1).Range.Text = oDRW("Text")
.Cells(1).WordWrap = True
.Cells(1).Width = 600
.Shading.BackgroundPatternColor =
WdColor.wdColorWhite
End With
c += 1
Next

Does anyone have any ideas why this is SO slow?!

Thanks,
Adam
 
J

Jay Freedman

Dealing with tables is *always* slow. The best method is to insert the cell
contents as plain text, with "columns" separated by tabs and with "rows"
separated by paragraph marks. When the whole thing is completely assembled,
then convert it to a table with something like

MyRange.ConvertToTable Separator:=wdSeparateByTabs

Have a look at the VBA help topic for this method.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

ACFalcon

Thanks that did improve it a lot, the only problem is that after conversion I
need all cells to word wrap, and some cells need to be resized and have their
colors changed. When I go to cycle through the table to do that it takes
forever and locks up the machine. Is there any other way to do this faster?
Maybe create the tables in an Excel spreadsheet then copy the spreadsheet
into the word document (I don't know if that is even possible). Does anyone
have any ideas?
 
H

Helmut Weber

Hi ACFalcon,

What is oDT? Another Table?
What oDRW? A row?
What's going on here:
For Each oDRW In oDT.Rows
With oWT.Rows(c)

You seem to loop over each row,
and in addition to loop over rows(.rows.count), but row 1.

To delete the first two cells of every row
except the first row,
one could try:

Sub Test6789()
Dim oCll As Cell
ActiveDocument.Tables(1).Columns(1).Select
selection.MoveRight Unit:=wdCharacter, _
Count:=1, Extend:=wdExtend
selection.start = selection.Cells(3).Range.start
'selection.Cells.Delete
ActiveDocument.Tables(1).Columns(1).Select
selection.start = selection.Cells(2).Range.start
For Each oCll In selection.Cells
oCll.WordWrap = True
' some more properties to be set
Next
End Sub

It seems you can't set wordwrap to the selection.cells.collection

In tables ranges are sometimes,
not so say most often, slower than the selection.

See:
http://tinyurl.com/y436j7

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

ACFalcon

oDT is a .NET Datatable and oDRW is a .NET Datarow. They are not slowing
down the process, because if I remove the editing of the word table
everything runs very quick.
 
H

Helmut Weber

Hi,
oDT is a .NET Datatable and oDRW is a .NET Datarow.
They are not slowing
down the process, because if I remove
the editing of the word table
everything runs very quick.

of course, the mere existence of them does
no harm, but what you do with them...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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