PDA

View Full Version : Right-align text on table columns 2-end



clhare
01-10-2008, 06:26 AM
I have a document with alot of tables in them. I want to select each table and right-align all but the first column, but I'm having trouble getting it to work right. I have the following code, but it is changing the alignment in all columns (instead of leaving the first column as is), and it's only updating the first table.

Sub CleanupTables()
Dim tblCurr As Table
Dim x As Integer
For Each tblCurr In ActiveDocument.Tables
With tblCurr
' Count total number of columns (minus 1 since 1st column isn't changed)
x = .Columns.Count - 1
' Right-align all but first column
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
.MoveRight Unit:=wdCell
.SelectColumn
.MoveRight Unit:=wdCharacter, Count:=x, Extend:=wdExtend
.ParagraphFormat.Alignment = wdAlignParagraphRight
End With
End With
Next

End Sub

What am I doing wrong?

fumei
01-10-2008, 10:06 AM
Cheryl, Cheryl.

"I want to select each table"

Select? You know better.
Sub AllTables()
Dim oTable As Table
Dim oCol As Column
Dim oCell As Cell
For Each oTable In ActiveDocument.Tables
For Each oCol In oTable.Columns
If oCol.Index <> 1 Then
For Each oCell In oCol.Cells
oCell.Range.ParagraphFormat.Alignment _
= wdAlignParagraphRight
Next
End If
Next
Next
End SubYou do need to use Selection.

You do not need to use a counter (if you are actioning all tables). You do not need a counter, or to count, as it does not matter if the table has 3 columns, or 5, or 6. For Each means just that, for each. Logically speaking, that meas EVERY, or ALL, items in that collection. For...Each

LOGIC:
For Each table in the document, For Each column in that table, IF the index of the column is NOT 1 - it is not the first column - For Each cell in that column...make it right-aligned.

Although...best-practice with Word would be to use Styles. Word is designed for styles. In which case, if you have an explicit style for certain cells of a table - which you should - then you would use the same code to make the cell THAT style. Say with a style named "TableRight", like this:Sub AllTables()
Dim oTable As Table
Dim oCol As Column
Dim oCell As Cell
For Each oTable In ActiveDocument.Tables
For Each oCol In oTable.Columns
If oCol.Index <> 1 Then
For Each oCell In oCol.Cells
oCell.Range.Style = "TableRight"
Next
End If
Next
Next
End SubVoila, every table column cell is formatted with the TableRight style, except for any cell in the first column.

fumei
01-10-2008, 10:50 AM
Here is a demo document that plays with formatting of tables via code. Click the "All Tables" icon/text/button on the top toolbar.

I made it toggle back and forth. Obviously in the real world, you would not normally do this. Therefore you would take that toggling logic out. I made it toggle just to demonstrate how easy - really - it is to format tables if you think about the chunks of them as things...as objects.

clhare
01-10-2008, 12:59 PM
I don't know why I'm always trying to use the selection object! I have to stop doing that!

The macro works great--the only thing I didn't realize was that I don't want to run it on tables 10 and 11. How would I exclude those two? I thought I could just add the code below (similar to the code that excludes column 1), but oTable does not have an index property as oCol does.

If otable.index <>10 and otable.index <> 11

fumei
01-14-2008, 11:26 AM
No, oTable can not have an Index if it is used as a For Each. For Each means, as I have stated, just that.

We are right back to a subject plugged many times. Specs. Stated requirements. What one really needs...really.

If you need only specifc tables to be processed, or NOT processed, then that is what you have to do.

Since you DO want exclude Tables 10 and Table 11 - and BTW, that seems to be a question of logic, a logic you do not state - then yes, you will have to have a counter.Sub SpecificTableCrap()
Dim aTable As Table
Dim TableCounter As Long
For Each aTable In ActiveDocument.Tables()
TableCounter = TableCounter + 1
If TableCounter = 10 Or TableCounter = 11 Then
MsgBox "This is table #10 or #11 " & _
"and I am not going to do anything."
Else
MsgBox "Gonna do something to Table: " & TableCounter
' do whatever....
End If
Next
End Sub

clhare
01-15-2008, 07:55 AM
Thank you for all your help! The macro is completed and works exactly the way I need it to. :thumb