PDA

View Full Version : formatting tables in Word



eed
09-03-2004, 08:18 AM
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! :blush

Anne Troy
09-03-2004, 09:03 AM
When in doubt, record!

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

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

:D

Anne Troy
09-03-2004, 09:04 AM
Doh! Sorry. You DID try recording. Wonder why it didn't work for ya?

eed
09-03-2004, 09:09 AM
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!!!

Anne Troy
09-03-2004, 09:14 AM
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.

eed
09-03-2004, 09:28 AM
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?

Anne Troy
09-03-2004, 09:34 AM
Sorry...choose Table from the list, and then hit the NEXT button.

eed
09-03-2004, 09:35 AM
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!

eed
09-03-2004, 09:45 AM
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:



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

Anne Troy
09-03-2004, 09:46 AM
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.

eed
09-03-2004, 09:51 AM
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...

Deanos
04-07-2010, 05:03 AM
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.

fumei
04-07-2010, 09:59 AM
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

fumei
04-07-2010, 10:01 AM
BTW: technically you code
wrdApp.Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=90, RulerStyle:= _
wdAdjustNone
wrdApp.Selection.Tables(1).Columns(2).SetWidth ColumnWidth:=54.45, RulerStyle:= _
wdAdjustNone
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.