A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Tables
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

AutoFit table via COM



 
 
Thread Tools Display Modes
  #1  
Old May 21st, 2004, 11:50 PM
Randolph Wang
external usenet poster
 
Posts: n/a
Default AutoFit table via COM

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.WdAutoFi tBehavior.wdAutoFitContent)
oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFi tBehavior.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
  #2  
Old May 23rd, 2004, 12:57 AM
macropod
external usenet poster
 
Posts: n/a
Default AutoFit table via COM

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.wdAutoFitC ontent)
.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitW indow)
End With

might help.

Cheers


"Randolph Wang" wrote in message
om...
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.WdAutoFi tBehavior.wdAutoFitContent
)

oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFi tBehavior.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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.684 / Virus Database: 446 - Release Date: 13/05/2004


  #3  
Old May 23rd, 2004, 07:37 AM
Cindy M -WordMVP-
external usenet poster
 
Posts: n/a
Default AutoFit table via COM

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.WdAutoFi tBehavior.wdAutoFitContent)
oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFi tBehavior.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 :-)

  #4  
Old May 25th, 2004, 09:10 PM
Randolph Wang
external usenet poster
 
Posts: n/a
Default AutoFit table via COM

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


"macropod" wrote in message ...
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.wdAutoFitC ontent)
.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitW indow)
End With

might help.

Cheers


"Randolph Wang" wrote in message
om...
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.WdAutoFi tBehavior.wdAutoFitContent
)

oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFi tBehavior.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



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.684 / Virus Database: 446 - Release Date: 13/05/2004

  #5  
Old May 25th, 2004, 09:15 PM
Randolph Wang
external usenet poster
 
Posts: n/a
Default AutoFit table via COM

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


Cindy M -WordMVP- wrote in message news:VA.000098a6.00010f0e@speedy...
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.WdAutoFi tBehavior.wdAutoFitContent)
oSelection.Tables(1).AutoFitBehavior(Word.WdAutoFi tBehavior.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 :-)

  #6  
Old May 28th, 2004, 05:38 PM
Randolph Wang
external usenet poster
 
Posts: n/a
Default AutoFit table via COM

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.
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 03:50 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.