Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Create asymmetrical table with macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rhino - 30 Jan 2006 22:47 GMT
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 ;-)

Signature

Rhino

Steve Yandl - 31 Jan 2006 01:00 GMT
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

> I'm trying to figure out how to create an asymmetrical table with a macro
> in Word 2002.
[quoted text clipped - 27 lines]
> make Draw Table possible again? Mind you, if I can't record the Draw Table
> process, I don't care that much ;-)
Rhino - 31 Jan 2006 05:14 GMT
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

> Rhino,
>
[quoted text clipped - 55 lines]
>> make Draw Table possible again? Mind you, if I can't record the Draw
>> Table process, I don't care that much ;-)
Tony Jollans - 31 Jan 2006 10:05 GMT
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

> Steve,
>
[quoted text clipped - 78 lines]
> >> --
> >> Rhino
Steve Yandl - 31 Jan 2006 14:52 GMT
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

> Steve,
>
[quoted text clipped - 76 lines]
>>> how to make Draw Table possible again? Mind you, if I can't record the
>>> Draw Table process, I don't care that much ;-)

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.