Getting the format of a table

M

Mystery Man

I have a situation where I create a table programmatically. I now want
to set the format of this table based on another table.

I have code similar to the following:

object oformat = (object)oldTable.AutoFormatType;
newTable.AutoFormat(ref oformat, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing);

As you can see, getting the format type of the old table is easy
enough. But how do I get the other properties, ie Apply Border, Apply
Shading, Apply Font etc from the oldTable? (These are the parameters
to the AutoFormat method).

Eg something like oldTable.Format.ApplyBorder


NB: I cannot copy and paste the table, and then write cell by cell as
this has performance implications. Therefore, I create the table
contents in text, convert the text to a table and then I want to
simply get the table format from the old table to apply it to the new
table.
 
C

Cindy M -WordMVP-

Hi Mystery,

Version of Word?

From what I can see, you'd need to apply the AutoFormat, then compare
the settings of the two tables. Then apply the AutoFormat again with
these settings.

Since you're having to do this, may one assume this is a document a user
will have been working with? What guarantee do you have the user won't
have made manual adjustments to the auto formatting?
I have a situation where I create a table programmatically. I now want
to set the format of this table based on another table.

I have code similar to the following:

object oformat = (object)oldTable.AutoFormatType;
newTable.AutoFormat(ref oformat, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing);

As you can see, getting the format type of the old table is easy
enough. But how do I get the other properties, ie Apply Border, Apply
Shading, Apply Font etc from the oldTable? (These are the parameters
to the AutoFormat method).

Eg something like oldTable.Format.ApplyBorder


NB: I cannot copy and paste the table, and then write cell by cell as
this has performance implications. Therefore, I create the table
contents in text, convert the text to a table and then I want to
simply get the table format from the old table to apply it to the new
table.

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 :)
 
M

Mystery Man

Thanks Cindy

The version of Word is 2000 and above.

How can I compare the settings of these two tables programmatically.
If I had access to these properties, then all would be solved.

No, the user does not modify the document. This happens all behinds
the scene. The user is simply presented with the end result.
 
C

Cindy M -WordMVP-

Hi Mystery,
The version of Word is 2000 and above.
That precludes using Table Styles, then.
How can I compare the settings of these two tables programmatically.
If I had access to these properties, then all would be solved.
You have the original table you want to match; you have the second
table formatted with the "default" AutoFormat. You compare each of the
possible things that could be affected and see if they match. If they
don't, the option was different.

If the borders are different, then ApplyBorders must have been "false"
when the original table was formatted. If the font formatting of the
first columns is different (bold vs. non-bold, usually), then
ApplyFirstColumn was "false" for the original table. Etc.
No, the user does not modify the document. This happens all behinds
the scene. The user is simply presented with the end result.
If that's the case, then why don't you already know what AutoFormat
was used on the original table?

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 :)
 
M

Mystery Man

Thanks Cindy
You have the original table you want to match; you have the second
table formatted with the "default" AutoFormat. You compare each of the
possible things that could be affected and see if they match. If they
don't, the option was different.

I apolgise if its a really dumb question, but how do I programatically
get access to the following properties in the original table:

ApplyBorders
ApplyShading
ApplyFont
ApplyColor
ApplyHeadingRows
ApplyLastRow
ApplyFirstColumn
ApplyLastColumn
AutoFit

I have checked the object model and no property is obvious to me.

The property AutoFormatType simply returns a WdTableFormat. This would
only be the first parameter in the call to the AutoFormat method when
setting up the new table.

Thanks for your patience
 
M

Mystery Man

Hi Cindy (or anyone else)

Sorry to keep asking, but how do I programatically get access to the
following properties in a table:

ApplyBorders
ApplyShading
ApplyFont
ApplyColor
ApplyHeadingRows
ApplyLastRow
ApplyFirstColumn
ApplyLastColumn
AutoFit

I have checked the object model and no property is obvious to me.

Thanks
 
S

Shauna Kelly

Hi Mystery Man

The macro recorder is very useful for learning about this kind of thing.
Tools > Macros > Record New Macro. Try inserting and formatting a table, and
then inspect the code that Word creates.

In particular, look for code like the following:

With Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
End With


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
C

Cindy M -WordMVP-

Hi Mystery,
Sorry to keep asking
I apologize: "real life" took me away from the groups for
quite a while...
do I programatically get access to the
following properties in a table:
You can't access what was applied or not applied using
AutoFormat (although I'd think that, since you
programmatically insert all the tables you'd KNOW that, and
could save the information somewhere to look up later).

But what you can do is compare the actual formatting of the
existing table and see if it matches the formatting of a
table inserted with all the defaults. Does it have the same
border colors? Same shading? Same font formattting? If not,
then the first was inserted with that aspect set to
"False".

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
:)
 
K

Kind writer/user/programmer

Thanks for the helpful suggestions of defining table behaviour with VBA.

I've been experimenting with the TableAutoFormat and discovered contention
between style definitions and Table AutoFormat definitions.

This new feature (Table Autoformat) bulldozes over styles. Thus, if I've set
the header row with a Table Heading style defined as bold centered text, when
I apply the TableTemplate, the first row bold setting in the TableAutoFormat
toggles this attribute to <not bold>.

Reveal formatting is willy-nilly:

TableFormat defines the paragraph alignment as left. The paragraph style is
defined as centered. Its centered. TableFormat defines the font style as
Arial. But I can't get the Bold to stick.

Selecting Clear Formatting in the Reveal Formatting pane (when the table is
selected), formats all the cells to be the selected Table style- I can't get
a paragraph or character style to display, but the paragaphs jump to 6 points
above and below--I presume based on Normal paragraph style. I didn't set
paragraph spacing in the Table AutoFormat, yet it pulled one out of somewhere.

How to erase/squelch/remove Table AutoFormat from a table? I can figure out
table to text, text to table--I want to know where to find--and alter-- the
hidden attributes for an entire table? Reveal all formatting doesn't permit
me to delete the table's AutoFormat setting--and "clear formatting" defaults
to the AutoFormat which has hidden controls for paragraph spacing, special
character formatting (i.e., <empahsis> on certain words).
 

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