PDA

View Full Version : Access and change table properties



Wellingburgh
03-19-2008, 07:39 AM
I have a large document, 106 pages, and each page contains a table with other tables nested inside. Basically they are too small. Some have a preferred width set of 15 cm which I want to be 17, others a preferred width of 14cm which I want to be 16, and future documents I have to work on will have some without the preferred with set at all. I thought it would be easy to create a macro that looped through all the tables in the document, test the value of preferred width and set it appropriately. I could then adjust it for every new document or table I had to work on. But I can't get it to work. I'm using Word 2000. So far the best I can do is:
Sub setTableWidth()
Dim aTable As Table
For Each aTable In ActiveDocument.Tables
If aTable.PreferredWidth = CentimetersToPoints(15) Then
aTable.Select aTable.PreferredWidthType = _
wdPreferredWidthPoints.aTable.PreferredWidth = CentimetersToPoints(17)
End If
Next aTable
End Sub
I can't see why this doesn't work, and I'd be very grateful if someone can enlighten me. I'm sorry the code is jumbled in above. I can't for the life of me get it to lay out properly.

Edit Lucas: vba tags added.....when posting code...select the code and hit the vba button

lucas
03-19-2008, 07:58 AM
paste your code directly from the visual basic editor in word.....select it and hit the vba button.....hope I didn't mess it up above....

Wellingburgh
03-19-2008, 09:53 AM
Thank you for that but...

The machine I am using is my home PC running Linux while the machine where I have the problem is my work machine and the sites I can access are restricted. So I had to copy by hand and then found I could not format it...

I'll do better next time - but meanwhile I am still struggling with this document full of nested tables and there are more to come!

I am grateful to you for making it readable.

fumei
03-19-2008, 10:08 AM
Are you using Option Explicit? If you are, you would get an "Invalid qualifier" compile error. The code is laid out incorrectly as well. You have:

aTable.Select aTable.PreferredWidthType = _
wdPreferredWidthPoints.aTable.PreferredWidth = CentimetersToPoints(17)

a one line.

aTable.Select can not be on the same line as aTable.PreferredWidth. It should be:


aTable.Select
aTable.PreferredWidthType = _
wdPreferredWidthPoints.aTable.PreferredWidth = CentimetersToPoints(17)


However, that will still get a parsing error as:
wdPreferredWidthPoints.aTable.PreferredWidth
is not valid syntax.

Further, did you look in Help?

"PreferredWidth - Returns or sets the preferred width (in points or as a percentage of the window width) "

Note there is NO mention of centimeters. PreferredWidth is always returned as points.

So....
If aTable.PreferredWidth = CentimetersToPoints(15)
will always return False.

Try looping through your tables like:
Dim aTable As Table
For Each aTable In ActiveDocument.Tables
MsgBox aTable.PreferredWidth
Next aTable
You will be surprised by what you see.

I know this is very very bizarre, but here is the correct code.
Sub setTableWidth()
Dim aTable As Table
For Each aTable In ActiveDocument.Tables
If Round(aTable.PreferredWidth / 28.35, 0) = 15 Then
aTable.PreferredWidth = CentimetersToPoints(17)
End If
Next aTable
End Sub

Warning! If you have manually changed the left/right edges of a table - at least in Word 2002 - the value of PreferredWidth will be 9999999. It STAYS as 9999999, as far as I know forever.

For example:

Make a new table.

PreferredWidth returns 0

Now nudge any vertical line - either an outside OR an inside.

PreferredWidth returns 9999999

Even though Table Properties still shows as Preferred width.......0 or greyed out.

I know of no way to ever change this. Somehow any manual change to a table permanently sets it.

MsgBox aTable.PreferredWidth (returns 9999999)
aTable.PreferredWidth = 0
aTable.PreferredWidth = CentimetersToPoints(17)
MsgBox aTable.PreferredWidth (returns 9999999)

So the formula above - Round(aTable.PreferredWidth / 28.35, 0) - will ONLY work if the table has never been manually adjusted.

Note that if the table has NOT been manually sized, but it HAS been adjusted (through the Table Proeprties) to a PreferredWidth, then you can use the Round(yadda yadda) method.

Bizarre. Crazy. I know. Tables are one of the strangest part of the Word Object Model.

Wellingburgh
03-19-2008, 10:51 AM
Gerry that is really helpful. There were some formatting errors in the code, the original was laid out correctly but I had an issue getting it copied here... Anyway, I did as you suggested and ran the msgBox - yes indeed a surprise.

Then I ran the code as you amended it with
If Round(aTable.PreferredWidth / 28.5, 0) = 16 and got exactly the result I wanted.

Then I tried substituting the width of the inner table (14cm) for 16 and ran it again but got no result. So I ran the msgbox again and got two values, 481.95 and 481.85. It looked odd to me that they were so close together, but I tried
if aTable.PreferredWidth = 481.85 then
aTable.PreferredWidth = CentimetersToPoints(16)
which got me nowhere. Just in case I tried the other value too with the same result.


So if those are the only two values, doesn't that mean that
For Each aTable in ActiveDocument.Tables
is not returning the inner of the nested tables, only the outer ones, where there is a .1pt difference in the preferred width?

As there are twice as many of these inner tables as the outer ones, I still have 66% of the job to do!

fumei
03-19-2008, 12:06 PM
"For Each aTable In ActiveDocument.Tables "

does NOT return inner nested tables.

say (for simplicity) you have TWO tables. Table_2 has TWO tables nested inside it.

ActiveDocument.Tables.Count = wait for it.....2

There are TWO tables in the document.

ActiveDocument.Tables(2).Tables.Count = 2

Table_2 has two tables, but the document has only TWO.....even though typographically speaking there are FOUR tables.

Table_1
Table_2 - the whole table
Table_2_A - the first table in Table_2
Table_2_B - the second in Table_2

This is connected to the ongoing discussion in the get full chapter name thread.

Word is NOT a typographical (layout) application. It is a word processor.

So while typographically there are FOUR tables in the document - you can see them - to Word...nope, there are TWO tables in the document.

If you have inner tables (and you do) then you have to run a test on each aTable.
Dim j As Long
Dim aTable As Table
For Each aTable In ActiveDocument.Tables
j = j + 1
If aTable.Tables.Count > 0 Then
j = j + aTable.Tables.Count
End If
Next
MsgBox "Total tables count = " & j

Or:
Dim j As Long
Dim aTable As Table
Dim Nested As Table
For Each aTable In ActiveDocument.Tables
If aTable.Tables.Count > 0 Then
For Each Nested In aTables.Tables
' whatever
End If
Next


Note that the table count OF a table = 0.
As I mentioned, tables in Word are not the prettiest object in the Object Model.

Wellingburgh
03-19-2008, 12:28 PM
Thanks, Gerry, I owe you.

So what I thought was going to be easy (access the tables collection and set the widths) turns out to be a nightmare. But from what you've given me, I can (I say "I can", I should say "if I was any good with VBA I could") loop through the tables, test to see if there is a nested table, and if there is and it has a certain preferred with, set it to the value I want using the 28.35:1 conversion.

The project I am working on is around Word documents produced automatically by Adobe Captivate - it's these latter that I create. I'm making demos for a web site for my employer, and the Word documents will be the manuals for the demos. There will be 5 demos (at the moment!) so 5 Word docs all with the same issue and all potentially hundreds of pages long, all with a table on every page containing nested tables. Each outer table has two inner tables and I need to resize the outers and both the inners. Oh well! Thank goodness I retire next year.

I wish I had your knowledge - I'll hit it again tomorrow, it's time to go eat now.

fumei
03-22-2008, 07:20 AM
"it's time to go eat now."

Yes, one must keep a proper glucose level.

Your project sounds a bit like a nightmare. Sorry, but I do not think there is any way around it. You are going to have to test each table and make any adjustments very carefully. Yuck. Bleeech.

Good luck. It is going to be a pain in the butt.

Dr@gonfly
10-10-2008, 10:34 AM
Better late than never?
I found this article while trying to find information on changing the text wrapping property of existing tables and thought that if anyone ever searched for this and got this post, it would be helpful!

http://msdn.microsoft.com/en-us/library/aa155744.aspx (it is also for 2002)