Consulting

Results 1 to 9 of 9

Thread: Access and change table properties

  1. #1

    Access and change table properties

    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:
    [vba]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[/vba]
    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

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3

    Thanks, but...

    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.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:

    [vba]
    aTable.Select
    aTable.PreferredWidthType = _
    wdPreferredWidthPoints.aTable.PreferredWidth = CentimetersToPoints(17)
    [/vba]

    However, that will still get a parsing error as:
    [vba]wdPreferredWidthPoints.aTable.PreferredWidth
    [/vba] 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....[vba]
    If aTable.PreferredWidth = CentimetersToPoints(15)
    [/vba]will always return False.

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

    I know this is very very bizarre, but here is the correct code.[vba]
    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 [/vba]

    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.

  5. #5

    Many thanks...

    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
    [VBA]If Round(aTable.PreferredWidth / 28.5, 0) = 16[/VBA] 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
    [VBA]if aTable.PreferredWidth = 481.85 then
    aTable.PreferredWidth = CentimetersToPoints(16)[/VBA]
    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
    [VBA]For Each aTable in ActiveDocument.Tables[/VBA]
    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!

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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.
    [vba]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[/vba]

    Or:
    [vba]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
    [/vba]

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

  7. #7

    Shheeesh

    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.

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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.

  9. #9
    VBAX Regular Dr@gonfly's Avatar
    Joined
    Feb 2008
    Location
    New York, NY
    Posts
    11
    Location
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •