Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

Speeding Up Word Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ACFalcon - 21 Nov 2006 15:56 GMT
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
Jay Freedman - 21 Nov 2006 16:34 GMT
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.

Signature

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.

> I have a fairly large Word document that a .NET page
> creates......between 2 and 10 MB depending on what the user selects.
[quoted text clipped - 39 lines]
> Thanks,
> Adam
ACFalcon - 21 Nov 2006 18:11 GMT
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?

> 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"
[quoted text clipped - 48 lines]
> > Thanks,
> > Adam
Helmut Weber - 22 Nov 2006 12:30 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

ACFalcon - 22 Nov 2006 14:21 GMT
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.

> Hi ACFalcon,
>
[quoted text clipped - 34 lines]
> See:
> http://tinyurl.com/y436j7
Helmut Weber - 22 Nov 2006 20:27 GMT
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...

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.