AutoFit table via COM

R

Randolph Wang

I am controlling Word 2003 via a VB.NET 2003 program.

The program creates an invoice using word and uses a table when it
prints the invoice detail.

At the conclusion of the outputting the table, I perform an autofit.
I autofit the table to the content and then autofit the table to
windows so the table will look nice.

oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent)
oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow)

At the beginning, the above commands do not work at all. The only way
I can get them to work is add a wait in between the commands.
Something similar to a doevents.

Even with that, the autofit will work only 75% of the time. It is not
failing because of something strange about the table because I can
generate the same invoice the next time and the table will autofit
properly.

Any clue as to how I can make it work every time?

Thanks in advance

Randy
 
M

macropod

Hi Randolph,

You may have more success setting the autofit properties before populating
the table. That way, Word doesn't need so much time at the end of the
process to work out the table's size - this can be done dynamically as the
table is populated. Also, changing your code to:

With oSelection.Tables(1)
.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent)
.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow)
End With

might help.

Cheers
 
C

Cindy M -WordMVP-

Hi Randolph,

Can't say I've ever encountered this problem. But then I'm not a huge fan of
AutoFit :) Is the Word document window fully visible when you send these
commands?

If it performs in "fits and starts", my guess would be that it could be a
question of the display having time to fully update. A longer table, in a more
complex document, can take measurably longer to finish layouting on-screen.
(<shudder> awful formulation, but I won't re-word it :))

You might try using the Range.Information property to get the vertical and
horizontal positions of the last cell of the table, before you execute the
first of these commands. Then use a Do/While loop that won't exit until this
changes (or lands on another page - better check that, as well). And put a
maximum time limit on it, for the rare case when nothing changes.
I am controlling Word 2003 via a VB.NET 2003 program.

The program creates an invoice using word and uses a table when it
prints the invoice detail.

At the conclusion of the outputting the table, I perform an autofit.
I autofit the table to the content and then autofit the table to
windows so the table will look nice.

oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent)
oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow)

At the beginning, the above commands do not work at all. The only way
I can get them to work is add a wait in between the commands.
Something similar to a doevents.

Even with that, the autofit will work only 75% of the time. It is not
failing because of something strange about the table because I can
generate the same invoice the next time and the table will autofit
properly.

Any clue as to how I can make it work every time?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
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 :)
 
R

Randolph Wang

This won't work because there is no content to auto fit against. The
reason why I perform an autofit content first is to let the column
proportion itself to the content. Then I perform the autofit to
window to let it distribute itself accordingly to the rest of the
window.

I moved the format code up and it is as if the command does nothing,
which is expected because there is no content.

Any other ideas?

Thanks
Randy
 
R

Randolph Wang

This is basically what I am doing.

I save the sizes of the columns and then perform the resize. Then I
will wait for a period of time. If the column size does not change, I
will do it again. But this time, I will wait twice as long. This
repeats itself for 5 times.

At this point, the configuration is set to 1 seconds per wait. So if
everything failed, the program would have waited 1 sec, 2 sec, 4 sec,
8 sec, and then 10 sec. COM is slow but not that slow.

Also, Word is not visible when this is performed.

The strange thing is that let say if the table failed to format
properly, I can close Word and ask the program to recreate the report.
9 out of 10 times the table will now be pretty.

There must be a fool proof way to do this...

Thanks
Randy
 
R

Randolph Wang

The last solution provided works. I found a bug in my program that
caused it not to wait more then a second. Once I changed the code to
wait properly (or longer if necessary), it never fails.

Thanks for all the help.
 

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