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 / Tables / May 2004

Tip: Looking for answers? Try searching our database.

AutoFit table via COM

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Randolph Wang - 21 May 2004 23:50 GMT
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
macropod - 23 May 2004 00:57 GMT
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

> I am controlling Word 2003 via a VB.NET 2003 program.
>
[quoted text clipped - 4 lines]
> 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.
[quoted text clipped - 10 lines]
>
> Randy
Randolph Wang - 25 May 2004 21:10 GMT
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

> Hi Randolph,
>
[quoted text clipped - 45 lines]
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.684 / Virus Database: 446 - Release Date: 13/05/2004
Cindy M  -WordMVP- - 23 May 2004 07:37 GMT
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.
>  
[quoted text clipped - 18 lines]
>  
> 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 :-)
Randolph Wang - 25 May 2004 21:15 GMT
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

> Hi Randolph,
>
[quoted text clipped - 43 lines]
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
Randolph Wang - 28 May 2004 17:38 GMT
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.
 
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.