Consulting

Results 1 to 14 of 14

Thread: formatting tables in Word

  1. #1
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location

    formatting tables in Word

    Hey, all,

    I have an html document (a table) which I am opening in Word 2000. I can perform all the normal, manual table modifications (deleting rows, resizing columns). I would like to be able to do these mods programmatically.

    I normally use the macro recorder in Word and then edit the macros as necessary, because I'm actually an Access VBA programmer and don't always know how to phrase my commands in Word. However, the macro recorder will not let me resize table columns!

    Can you select a table and modify the column widths through VBA in Word? What would the proper command syntax be? Thanks in advance for any help!
    With program specs this fickle, you've just got to believe in Discord.

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    When in doubt, record!

    [vba]Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 9/3/2004 by Anne Troy
    '
    Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints
    Selection.Tables(1).Columns(1).PreferredWidth = InchesToPoints(2)
    Selection.Move Unit:=wdColumn, Count:=1
    Selection.SelectColumn
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(2)
    Selection.Move Unit:=wdColumn, Count:=1
    Selection.SelectColumn
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(2)
    Selection.Move Unit:=wdColumn, Count:=1
    Selection.SelectColumn
    Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
    Selection.Columns.PreferredWidth = InchesToPoints(2)
    Selection.Move Unit:=wdColumn, Count:=1
    Selection.SelectColumn
    End Sub[/vba]

    Of course, that's recorded junk, but if you're a programmer, you ought to be able to figure it out? I hope!

    ~Anne Troy

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Doh! Sorry. You DID try recording. Wonder why it didn't work for ya?
    ~Anne Troy

  4. #4
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    Quote Originally Posted by Dreamboat
    Doh! Sorry. You DID try recording. Wonder why it didn't work for ya?
    I don't know, my cursor turned into a cassette tape icon and I couldn't really do anything with my mouse inside the table (couldn't click into a new cell, select rows, grab columns, etc.). I could navigate around with the arrow keys, but couldn't find a way to grab the columns using the keyboard. Anyway, the code you pasted looks great, and I'm pretty sure I'll be able to modify it to match my uses. If I have any problems, I'll let you know. Thanks!!!
    With program specs this fickle, you've just got to believe in Discord.

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    If you need to select the table while recording (I already had my cursor inside the table when I started recording), then use F5 (Edit-Go to) and choose table...that'll take you to the table and you can likely record what you need.
    ~Anne Troy

  6. #6
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    Quote Originally Posted by Dreamboat
    If you need to select the table while recording (I already had my cursor inside the table when I started recording), then use F5 (Edit-Go to) and choose table...that'll take you to the table and you can likely record what you need.
    Okay, this may be a silly question. The Edit - Go To command keeps asking me for a Table Number. What on earth is the table number?
    With program specs this fickle, you've just got to believe in Discord.

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Sorry...choose Table from the list, and then hit the NEXT button.
    ~Anne Troy

  8. #8
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    Ooookay, I feel kind of stupid. I've got it now. I couldn't grab the black borders between the columns, like I can when I'm not in the macro recorder, but I CAN grab the little column boundary indicators up on the ruler (like where the tab stops are). I didn't have the ruler turned on earlier... now I can drag and record as I need to.

    Thanks for all the help. This is solved now!
    With program specs this fickle, you've just got to believe in Discord.

  9. #9
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    Okay, I know my whole procedure is starting to sound really complicated, BUT:

    I'm running all this code to control Word from inside Access. I could edit the columns just fine in Word, but when I ran my code to modify the table column widths from Access, I got this error message:


    5992: Cannot access individual columns in this collection because the table has mixed cell widths.


    Do you think this is because the table is an HTML document? Is this just a lost cause? The VBA I used is below:


    [VBA]
    wrdApp.Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=90, RulerStyle:= _
    wdAdjustNone
    wrdApp.Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=54.45, RulerStyle:= _
    wdAdjustNone
    wrdApp.Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=47.3, RulerStyle:= _
    wdAdjustNone
    wrdApp.Selection.Tables(1).Columns(4).SetWidth ColumnWidth:=303.25, RulerStyle:= _
    wdAdjustNone

    [/VBA]
    With program specs this fickle, you've just got to believe in Discord.

  10. #10
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Can you upload your file? Or even part of it?

    Also, you don't need to use the font codes when posting VBA. It does it all by itself.
    ~Anne Troy

  11. #11
    VBAX Contributor
    Joined
    Jun 2004
    Location
    Texas
    Posts
    139
    Location
    The font code thing was an accident, I know I don't need to include those.

    I can't upload my working file, because 99.999% of what it contains is pretty proprietary. I'll copy it and try to trim it down to an illustrative rendition, and I'll post it after lunch today.

    Thanks for all the help, sometimes I'm completely hopeless on the smallest things...
    With program specs this fickle, you've just got to believe in Discord.

  12. #12
    VBAX Newbie
    Joined
    Apr 2010
    Posts
    1
    Location

    F5? Help

    I'm struggling, I'm trying to do something pretty much the same as you. I've built the table in VBA. I can't select while in recorded macro mode. I've tried F5 but only the first row in the table is selected. I'm trying to left align the whole table. Any help would be appreciated. Cheers.

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    The error means exactly what it says. If the cells are different in dimension in ANY way by column, then:

    5992: Cannot access individual columns in this collection because the table has mixed cell widths.

    VBA can not, and never has ever been able to, action table elements if the cells are different, be it merged, or variable dimemsions. They can be variable dimensions by column in any given row, but NOT by row.

    In other words (random numbers):

    Different by column...OK
    Row1 Col1 (100 pts) Col2 (70 pts) Col3 (400 pts)

    Different by ROW...VBA will NOT work
    Row1 Col1 (100 pts) Col2 (70 pts) Col3 (400 pts)
    Row2 Col1 (100 pts) Col2 (100 pts) Col3 (370 pts)

    I.e. Cannot access individual columns in this collection because the table has mixed cell widths

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    BTW: technically you code[vba]
    wrdApp.Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=90, RulerStyle:= _
    wdAdjustNone
    wrdApp.Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=54.45, RulerStyle:= _
    wdAdjustNone
    [/vba]is fine (although it is better to not use Selection), but the error implies that this table has more than one row, and the other row(s) do NOT have these dimensions.

Posting Permissions

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