More Word Optimization: Table Building

E

Evan Stone

Hello,

I'm getting really poopy performance from Word automation when building
tables dynamically (i.e. adding table with x cols, then adding rows
one-by-one), and I was wondering if there were any ways to make it a bit
more snappy...

One thing I was musing on was... is it possible to build the table in HTML
(or XML) and then insert it into Word? Would Word be smart enough to convert
it into a table?

Thanks in advance.

evan stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
I'm getting really poopy performance from Word automation when building
tables dynamically (i.e. adding table with x cols, then adding rows
one-by-one), and I was wondering if there were any ways to make it a bit
more snappy...
Oh, yes, indeed! You'll find an article on the topic on the MSDN website. I
can't recall whether any .NET code is in it, but the principles remain the
same, in any case.
One thing I was musing on was... is it possible to build the table in HTML
(or XML) and then insert it into Word? Would Word be smart enough to convert
it into a table?
HTML: only if you write it to a separate file, then use Insert/File to trigger
the converter

XML: definitely, as long as we're talking Word 2003 ONLY (no support in
earlier versions). You'd need to write the table in WordProcessingML, then use
the Range.InsertXML "xml string" method. This would, without question, be the
fastest method in execution. Especially if you require a lot of formatting.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
E

Evan Stone

Hi Cindy,
Oh, yes, indeed! You'll find an article on the topic on the MSDN website. I
can't recall whether any .NET code is in it, but the principles remain the
same, in any case.

You wouldn't happen to have a link handy would you? I found this but didn't
know if it was the article you were referring to (concatenate the two lines
of the URL if necessary):

http://msdn.microsoft.com/library/d...03_ta/html/OfficeWordAutomatingTablesData.asp

I'd really like to find out how to optimize table building if there's a way
to speed it up. I generated a table that was about 21 pages long, and it
took several minutes to complete on my 3GHz P4 machine. I shudder to think
what it would do on a Celeron. ;)

And I'm totally not concerned with whether it's .NET or not. I've been
converting back and forth throughout this whole project, so I'm getting
pretty savvy at the conversion... IOW, any information will help.
HTML: only if you write it to a separate file, then use Insert/File to trigger
the converter

Grr... OK, well I'll keep that in mind, and perhaps I can use this method
for HUGE tables, but build other tables line by line.
XML: definitely, as long as we're talking Word 2003 ONLY (no support in
earlier versions). You'd need to write the table in WordProcessingML, then use
the Range.InsertXML "xml string" method. This would, without question, be the
fastest method in execution. Especially if you require a lot of
formatting.

Grrr... (again). Naturally, I'm working with Word 2000+, so it looks like
XML is out. What a bummer.

Thanks for your help, Cindy!

evan stone | software engineer
 
C

Cindy M -WordMVP-

Hi Evan,
You wouldn't happen to have a link handy would you? I found this but didn't
know if it was the article you were referring to (concatenate the two lines
of the URL if necessary):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_wd2003_ta/ht
ml/OfficeWordAutomatingTablesData.asp
Yes, that's the article I meant (and I see it does have some C# in there :))

Given that this is for 2000 and upwards, concentrate on the "concatenate it into
a string, assign tthat to a range, then ConvertToTable method" stuff. That is the
fastest method.
Word.Table tbl = rng.ConvertToTable(some params here)

Special attention to inserting it with the Word97 (8.0) defaults, as these
suppress time-consuming layout processes.

Then format the table as required, using the tbl object. Note that if the
formatting is fairly standard, that using STYLES will help speed things up.
Unfortunately, table styles aren't supported in Word 2000. But even paragraph and
character styles can save you quite a bit of coding/execution time and effort.

Do any column/row sizing as the LAST step.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
E

Evan Stone

Given that this is for 2000 and upwards, concentrate on the "concatenate
it into
a string, assign tthat to a range, then ConvertToTable method" stuff. That is the
fastest method.
Word.Table tbl = rng.ConvertToTable(some params here)

LOL. Funny you should mention that!

A coworker suggested this method to me yesterday at a meeting (I didn't even
know Word had such a feature!), so I just recorded a macro, converted it to
C# code, and away it went (OK, it was a bit more involved than that, because
I had to write a routine that built out a tab-delimited table, but you get
the idea...)!

By the end of the day, the table that was 21 pages long and taking about 6
minutes to build is now taking 6 *seconds*!!!

Which, is, um... pretty awesome to say the least. So I'm glad we were
thinking along the same lines... very reassuring. ;)

Thanks again, Cindy!

:D

evan k. stone | software engineer
 
A

annica

Hi Evan,

I also have problem with tables being very slow, what exactly did your code
look like?
What was the differens to your previous code?

Hope you can help me :)

Thank you
/annica
 

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