Word 2000/97: Copy the format of an existing Table

V

VBA Coder

I have 2 tables in my Word Document (template), and I'd like to be able to
copy the formatting from the 1st table (ie: Border Style, Shading, Font,
etc...) to the 2nd table using VBA code. I know I can use the .Duplicate
function of a paragraph to copy its format and apply it to another
paragraph, but can this be done using Tables?

Thank you.
 
W

Word Heretic

G'day "VBA Coder" <[email protected]_SPAM.>,

This is a nightmare mate - you have to iterate each border in each
table part.


Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


VBA Coder was spinning this yarn:
 
V

VBA Coder

Yes, I did have to go through each Border, but it actually worked out to be
not so bad. 1st, create a Table object and set it to the NewTable (the
table you want to set the formatting to). Then create a Range object that
will refer to the Original Table that you are going to copy the formatting
from.

My sample code is below to set the formatting of one table to the formatting
of another: I only did a few properties, but it gives you the idea.

Dim myTable As Table, rRange As Range

' Use a Table object to refer to the 2nd Table that we will apply formatting
to
Set myTable = ActiveDocument.Tables(2)
' Set a Range object to the 1st Table that we want to copy the formatting
from
Set rRange = ActiveDocument.Tables(1).Range
With myTable
.Borders.OutsideLineStyle = rRange.Borders.OutsideLineStyle
.Borders.InsideLineStyle = rRange.Borders.InsideLineStyle
.Rows(1).SetHeight RowHeight:=rRange.Rows(1).Height,
HeightRule:=rRange.Rows(1).HeightRule
End with
 
W

Word Heretic

G'day "VBA Coder" <[email protected]_SPAM.>,

Mate, what about a table like this:


================
|| Col 1 | Col2 | col3 ||
================
|| | | ||
================

Not so easy. A short piece of string is a lot easier to make than a
long one.

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


VBA Coder was spinning this yarn:
 
V

VBA Coder

Use the same method as I described, but you will need to read some
additional border properties. Some sample code to do some formatting to the
various borders of a table within my app is below. It should give you the
jist of how to set different formatting properties:

With oTable
.Borders(wdBorderLeft).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders(wdBorderRight).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders(wdBorderTop).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders(wdBorderBottom).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders(wdBorderHorizontal).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders(wdBorderVertical).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders(wdBorderDiagonalDown).LineStyle = IIf(bBorder,
wdLineStyleSingle, wdLineStyleNone)
.Borders(wdBorderDiagonalUp).LineStyle = IIf(bBorder, wdLineStyleSingle,
wdLineStyleNone)
.Borders.Shadow = False
End With
 
W

Word Heretic

G'day "VBA Coder" <[email protected]_SPAM.>,

LOL - close - but not quite.

you set the table, which must have three rows, then format the first
row and first column sep. About two pages of code - which is 1.9 pages
more than it needs to be :)

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


VBA Coder was spinning this yarn:
 

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