Create asymmetrical table with macro

R

Rhino

I'm trying to figure out how to create an asymmetrical table with a macro in
Word 2002.

The table needs exactly two rows in it: on the first row, there are 4
equal-sized cells (I'll actually need to resive them a bit but equal-sized
will do for now); on the second row, there is a single cell that uses the
full width of the table. The table itself will be the full width of the page
less the left and right margins. I'm putting 4 small facts in the first row
and a paragraph in the second row.

I've figured out two ways to do it in Word via the GUI:
1. Use Draw Table to create a new table; draw a horizontal line to tell Word
that I want two rows; draw dividers on the first row to create 4 cells in
that row.
2. Use Insert/Table to create a new table of 4 columns and 2 rows. Then,
select the 4 cells in the second row, click on the boxed plus sign that
appears above and left of the top, leftmost cell of the table, right-click
to get a context, then select Merge Cells.

Unfortunately, neither approach can be recorded in the macro recorder. I've
managed to record the first half of the second approach, namely the creation
of the table with 4 columns and 2 rows, but I can't record the process to
merge the cells and haven't any idea how to do it with a VBA macro. After
creating a table with the second approach, the first approach doesn't work
at all: Draw Table is greyed out and I can't figure out how to ungrey it.

Can anyone tell me how a macro would accomplish the creation of the table
entirely with VBA statements? For bonus points, can you explain how to make
Draw Table possible again? Mind you, if I can't record the Draw Table
process, I don't care that much ;-)
 
S

Steve Yandl

Rhino,

I used the macro recorder to build the sub for creating a two row by four
column table. I then inserted a single line (it will be third from the
bottom) to merge the cells in the second row. Note that if you ultimately
have more than one table you can't assume you will be working with the table
having index number 1 all the time.

Sub MyNewTable()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.Rows(2).Cells.Merge
End With

End Sub


Steve
 
R

Rhino

Steve,

Your code works fine for my first table. The first row has four cells, the
second row has a single cell and my 5 values each land in the correct cell.

But I have 6 consecutive tables with the same structure and when I try to
create the second table, I get:

Run-time error '4605': This method or property is not available because
the object refers to the end of a table row.

What do I need to change to be able to create and populate the 2nd through
6th tables? I thought I could possibly solve the problem by incrememnting
the index in the "With Selection.Tables(1)" line through the range 0 through
5 but that didn't work and neither did incrementing the index through the
range 1 through 6. I'm not sure what else to try.

---
Rhino

Steve Yandl said:
Rhino,

I used the macro recorder to build the sub for creating a two row by four
column table. I then inserted a single line (it will be third from the
bottom) to merge the cells in the second row. Note that if you ultimately
have more than one table you can't assume you will be working with the
table having index number 1 all the time.

Sub MyNewTable()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:=
_
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.Rows(2).Cells.Merge
End With

End Sub


Steve
 
T

Tony Jollans

To make sure you work with the table you've just created, set a reference to
it when you create it. You can use that reference instead of the explicit
reference to table #1.

Change:

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,
NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)

Change:

Set NewTable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=2, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)
With NewTable

--
Enjoy,
Tony


Rhino said:
Steve,

Your code works fine for my first table. The first row has four cells, the
second row has a single cell and my 5 values each land in the correct cell.

But I have 6 consecutive tables with the same structure and when I try to
create the second table, I get:

Run-time error '4605': This method or property is not available because
the object refers to the end of a table row.

What do I need to change to be able to create and populate the 2nd through
6th tables? I thought I could possibly solve the problem by incrememnting
the index in the "With Selection.Tables(1)" line through the range 0 through
5 but that didn't work and neither did incrementing the index through the
range 1 through 6. I'm not sure what else to try.
 
S

Steve Yandl

Rhino,

Tony's solution above gives you the most control as you're creating the
tables.

If you already have a set of tables created and want to process the entire
set, merging the cells in row 2 on each one, you could have something like
the following.

Sub MergeRowTwo()

Dim oTable As Table
For Each oTable In ActiveDocument.Tables
oTable.Rows(2).Cells.Merge
Next oTable

End Sub

Steve



Rhino said:
Steve,

Your code works fine for my first table. The first row has four cells, the
second row has a single cell and my 5 values each land in the correct
cell.

But I have 6 consecutive tables with the same structure and when I try to
create the second table, I get:

Run-time error '4605': This method or property is not available because
the object refers to the end of a table row.

What do I need to change to be able to create and populate the 2nd through
6th tables? I thought I could possibly solve the problem by incrememnting
the index in the "With Selection.Tables(1)" line through the range 0
through 5 but that didn't work and neither did incrementing the index
through the range 1 through 6. I'm not sure what else to try.
 

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