need help with table code

M

Michael F

Hi,

I got some code from a userform for creating and formatting a table (found
it in an old Word template). After adding the table, it uses the Selection
object to select that table for formatting and then to select the top row for
formatting.

What is the proper method for selecting parts of a table you just created or
happen to be inside? I read somewhere on the MVPS.org site about not using
Selection so much. I tried using ActiveDocument.Tables(1).Rows(1).Select to
select a row but it kept jumping to the first table in the file or giving me
an error if I left out one of the index numbers. I don't understand how that
syntax works or even if it's possible to use ActiveDocument to select a table
or parts of one.

And of course the Word VBA online help told me nothing about how to add a
table or find parts of it.

Michael F.


' add a table using numbers from the form
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=txtRows, _
NumColumns:=txtColumns

'Select the entire table
Selection.Tables(1).Select
'And apply Table Cell style to all
Selection.Style = ActiveDocument.Styles("Table cell")

'select the first row and apply table heading style
'ActiveDocument.Tables(1).Rows(1).Select
Selection.Rows.First.Select
Selection.Style = ActiveDocument.Styles("Table heading")

'Leave cursor in first cell
Selection.HomeKey Unit:=wdLine
 
J

Jonathan West

Hi Michael

Michael F said:
Hi,

I got some code from a userform for creating and formatting a table (found
it in an old Word template). After adding the table, it uses the
Selection
object to select that table for formatting and then to select the top row
for
formatting.

What is the proper method for selecting parts of a table you just created
or
happen to be inside?

Learn to loive the Range object. Just about every component of a Word
document has a Range property, and you can do most of the same things to it
as you can do to a Selection. The Range has two great advantages

1. There are lots of them, but only one Selection

2. It is usually quicker to use a Range.
I read somewhere on the MVPS.org site about not using
Selection so much. I tried using ActiveDocument.Tables(1).Rows(1).Select
to
select a row but it kept jumping to the first table in the file

Look at that line of code and you'll see why. Read it backwards. You are

- Selecting
- the first
- Row
- of the first
- Table
- Of the active document.
or giving me
an error if I left out one of the index numbers. I don't understand how
that
syntax works or even if it's possible to use ActiveDocument to select a
table
or parts of one.

And of course the Word VBA online help told me nothing about how to add a
table or find parts of it.

Michael F.

Below each of your lines I'll put the equivalent code that doesn't need to
use the Selection

' add a table using numbers from the form
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=txtRows, _
NumColumns:=txtColumns

Dim oTable as Table
Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=txtRows, _
NumColumns:=txtColumns)
'Select the entire table
Selection.Tables(1).Select
'And apply Table Cell style to all
Selection.Style = ActiveDocument.Styles("Table cell")

oTable.Range.Style = "Table cell"
'select the first row and apply table heading style
'ActiveDocument.Tables(1).Rows(1).Select
Selection.Rows.First.Select
Selection.Style = ActiveDocument.Styles("Table heading")

oTable.Range.Rows.First.Range.Style = "Table heading"
'Leave cursor in first cell
Selection.HomeKey Unit:=wdLine

Your code (not including comments) was 6 lines. Mine was 4, including the
declaration, and it will run a good bit faster, though you would need a much
longer bit of code for the speed difference to become apparent.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

Michael F

Jonathan,

Thanks for the reworked code. I tried it one statement at a time to
understand what each statement did. I need a recommendation for a good book
on VBA for Word.

How is it that the Set oTable... statement actually creates a table rather
than just storing it in oTable? I could not find any online help that
explained what Dim oTable as Table meant other than creating a variable.

In the next statement, it looks like oTable is an object and you are setting
the style for the range that is the table. Did I get that right?
oTable.Range.Style = "Table cell"

In the next statement that applies a style to the first row, I could not
figure out how you came up with it.
oTable.Range.Rows.First.Range.Style = "Table heading"

I tried using the VBA trick of entering a string then a period and letting
VBA prompt with a list of valid terms, but I could not quite figure out how
you figured out the path.

I have two other settings I would like to make:
- setting the table indent
- setting the table width

I was able to figure out how to set the indent with
oTable.Rows.LeftIndent = InchesToPoints(0.1)
but I tried oTable.PreferredWidth = "4.75" for the table width and it jammed
all the columns into the smallest width (less than an inch).
 
J

Jonathan West

Michael F said:
Jonathan,

Thanks for the reworked code. I tried it one statement at a time to
understand what each statement did. I need a recommendation for a good
book
on VBA for Word.

Book reviews here

Book Recommendations
http://www.word.mvps.org/Tutorials/BookRecommendations.htm
How is it that the Set oTable... statement actually creates a table rather
than just storing it in oTable?

This line creates a table in the document

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=txtRows, _
NumColumns:=txtColumns

This line creates the same table in the document, and also says, in effect
"for the prupsoes of this macro, this table is called oTable. When you want
to refer to anything in this table, you can use this name."

If you know that the table is the first table in the document, then oTable
is exactly the same as ActiveDocument.Range.Tables(1). The advantage of usng
oTable this way is that you now have a reference to the table, wherever it
is, and it will continue to point to that specific table even if you insert
other tables elsewhere in the document.

I could not find any online help that
explained what Dim oTable as Table meant other than creating a variable.

Yes, it is creating an object variable. The specific object is a Table
obejct.
In the next statement, it looks like oTable is an object and you are
setting
the style for the range that is the table. Did I get that right?
oTable.Range.Style = "Table cell"

Pretty close. The oTable object has a Range property, which in turn is a
Range object which has a Style property, and we are assigning a new name for
that style.
In the next statement that applies a style to the first row, I could not
figure out how you came up with it.
oTable.Range.Rows.First.Range.Style = "Table heading"

read backwards again. We are

- giving the name "Table heading"
- to the Style
- of the Range
- of the First
- Row
- of the range
- of the table pointed to by the oTable variable

(Actually, one of those ranges is superfluous, the Table objact has a Rows
property directly, we don't need a Range in there. So that line could be as
follows
oTable.Rows.First.Range.Style = "Table heading")
I tried using the VBA trick of entering a string then a period and letting
VBA prompt with a list of valid terms, but I could not quite figure out
how
you figured out the path.

Lots of practice :)
I have two other settings I would like to make:
- setting the table indent

oTable.Rows.LeftIndent = CentimetersToPoints(1.5)
- setting the table width

'set a fixed width
oTable.PreferredWidthType = wdPreferredWidthPoints

'decide what the width should be
oTable.PreferredWidth = CentimetersToPoints(16)
I was able to figure out how to set the indent with
oTable.Rows.LeftIndent = InchesToPoints(0.1)
but I tried oTable.PreferredWidth = "4.75" for the table width and it
jammed
all the columns into the smallest width (less than an inch).

Almost every dimension is specified in points in Word VBA, and a table that
is only 4.75pt wide is pretty narrow!

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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