Macro for nested tables

G

Gandalf Baggins

I have inherited an automatically produced Word 2000 document which
contains many nested tables. There are over 100 outer tables which do
not have the preferred width set and each of these has several inner
tables, where it is set. And I need to change the settings.

So I thought what I'd do was the following

get the set of tables in the document
loop through these tables
if preferred width not set
set it 15 centimetres
if preferred width set to 7 cm
set it to 8 cm
if preferred width set to 10 cm
set it to 14 cm
exit

That looks quite trivial - so when I tested it with looking for tables
where the preferred width was not set (on a copy of the document) I was
dismayed to get no result. I tried testing for a preferred width that
was null and also for it being set to zero, but came up with nothing...

My code looks like this:

Sub setTableWidth()

Dim thisDoc
Dim aTable

Set this.Doc = ActiveDocument

For each aTable in thisDoc.Tables
aTable.PreferredWidthType = wdPreferredWidthPoints
if aTable.PreferredWidth = Null Then
aTable.PreferredWidth = CentimetersToPoints(15)
End If
Next aTable
End Sub

If I can't get beyond this stage I have no hope of the end point I have
in view.

Obviously my lack of familiarity with the Word Object Model and/or VBA
syntax is letting me down. Any help will be received very gratefully.
 
H

Helmut Weber

Hi Gandalf,

this works for me:

Sub Test561()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.PreferredWidth = 0 Then
oTbl.Select ' for testing
oTbl.PreferredWidthType = wdPreferredWidthPoints
oTbl.PreferredWidth = CentimetersToPoints(15)
MsgBox PointsToCentimeters(oTbl.PreferredWidth)
' watch out, this returns "15,00011"
' with komma as decimal seperator here
'End If
Next
End Sub

As the width of tables is calculated in points
you get no straight values in centimeters.

You might need something like:

MsgBox Round(PointsToCentimeters(oTbl.PreferredWidth))
or
if Round(PointsToCentimeters(oTbl.PreferredWidth)) = 15 then

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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