Macro to Evenly Distribute Rows on Several Tables

R

Rashid Khan

Hello All,
I am having a document with several hundred tables and I wish to set the
'Distribute Rows Evenly' for each and every table. I recorded a macro and
tried to modify it as follows but I failed. Can anyone help me out!


Dim oTbl As Word.Tables
For Each oTbl In ActiveDocument.Tables
Selection.Cells.DistributeHeight <it gives error here>
Next
End Sub

What am I doing wrong? Basically I always need to run a macro on several
hundred tables to do different things. So a standard macro would also help
me out.

TIA
Rashid Khan
 
J

Jay Freedman

Hi Rashid,

You have several errors in that code. Here's the corrected version:

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.Rows.DistributeHeight
Next

In the Dim statement, declare the variable as 'Table', not
'Word.Tables' (the plural refers to a collection).

Instead of Selection, the statement inside the For loop must refer to
the oTbl that's used as the loop variable. (The recorder always uses
the Selection -- that's one of its serious drawbacks.) Then the
..DistributeHeight method belongs to the table's .Rows property, not
..Cells.
 
R

Rashid Khan

Hi Jay,
The macro gives an error on the following line:Can u suggest a fix?
Thanks for your time. I tried to run the macro with and without selecting a
table. But it gives an error on both cases. I need the macro to run on all
my Tables in the Active Document and set 'Distribute Rows Evenly' for each
and every Table without me selecting it. I hope I am clear now.

Rashid Khan
 
J

Jay Freedman

Hi Rashid,

I did test that code before I posted it, and it works on my PC. What was the
exact text of the error message you saw?
 
R

Rashid Khan

Hi Jay,
It says "Run Time Error - 4198 : Command Failed" and highlights the line I
mentioned earlier.

BTW, I am using Office XP.

Rashid Khan
 
J

Jay Freedman

Hi Rashid,

Hmm, that's not a very helpful message, is it?

I suspect the problem is not with the code so much as with the contents of
the document, and the code not being able to handle it. Do any of your
tables have either merged or split cells (although my test shows that the
command still works in that case)? Does the menu command Table > Autoformat
Distribute Rows Evenly work on your tables? It's possible there's a
corrupted table in the document -- see
http://word.mvps.org/FAQs/AppErrors/CorruptDoc.htm.
 
R

Rashid Khan

Hi Jay,
I am not sure, but anyhow thanks for your help.. I would visit the URL u
have sent to me.

Rashid Khan
 

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