How to handle tables with merged cells?

K

Ken Wan

Hi,
When a table containing merged cells, the follwing code will causing
run-time error at the line start For loop.
If the cells are merged horizontally:
For Each aCol In objTable.Columns
processOneColumn aCol
Next
If the cells are merged vertically:
For Each aRow In objTable.Columns
processOneRow aRow
Next

How should I handle these situations?
Any suggestion will be greatly appreciated.
Ken
 
J

Jay Freedman

Hi, Ken,

There really isn't any good way to deal with merged cells -- or even to know
which rows or columns contain merged cells.

It depends on what you want to do to the table. For example, if you want to
act on each cell, where you would normally do a nested loop like

For Each aCol In objTable.Columns
For Each aCell in aCol.Cells
' do something in aCell
Next aCell
Next aCol

(or the equivalent per row), you can instead step through the cells without
referring to rows or columns:

Dim aCell As Cell
Set aCell = objTable.Cell(1, 1)
Do
' do something in aCell
Set aCell = aCell.Next
Loop Until aCell Is Nothing

If you need to do something to the column (or row) as a whole, you're really
in deep trouble. You may need to know something about the formatting in
advance (for example, if the table was created by another macro or another
application, it may be the same in all documents) and program to that
specific format -- but remember to check the assumptions and use error
trapping.

If you're extracting information from the table without altering it, it's
possible to convert the table to text, grab what you need from the resulting
mess, and then Undo to put the table back together.
 
J

Jay Freedman

Hi, Ken,

That's a tough problem. Have you tried using Table > Convert Table To Text?

If that doesn't work, you'll have to make two passes through the table: one
to find the longest line in each column (that is, the one with the most
characters, since you'll be converting from a proportional font to a
nonproportional one), and another to insert the padding spaces or tabs. If
the table may have merged cells, you need to figure out which columns go
where.
 
K

Ken Wan

Hi, Jay,
Thanks for the advice.
I did try to use table.convertToText. However, the results were not I
wanted. In fact, the result of using table.convertToText is different from
using menu Table->Convert->table to text .... I may have to do just what you
suggested to pad the table cells then convert.
Ken
 

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