PDA

View Full Version : column width & text alignament in cells



apostolu
10-07-2011, 04:08 AM
I have a macro that is suposed to modify the colum width and align text in cells :)
Of course, I got the :
"Run-time error 5992. Cannot access individual columns in this collection because the table has mixed cell widths", error.

Someone on the net told me that eaven I "design" the tables in Office, I will never get the same width of the cells, in the same column.
I tried the macro with Win 2k & Office 2k and XP & Office 2010 and I got the same error.


Sub ResizeAllTables()
Dim tbl As Table
Dim i As Integer

'Defineste matricea pentru dimensiunile coloanelor
' (in acest exemplu - 3 linii pentru ca vom modifica numai 3 coloane din tabelele "desenate")
'Coloana 1 este pentru numarul coloanei ce va fi modificata
'Coloana 2 este pentru a stabili dimensiunea coloanei (in procente)
'Coloana 3 este pentru a stabili modalitatea de aliniere pe orizontala
'Coloana 4 este pentru a stabili modalitatea de aliniere pe verticala

Dim arrCols(1 To 3, 1 To 4) As Long

'Setari COL nr.1
arrCols(1, 1) = 2 'Numarul coloanei din tabel care de modificata (2)
arrCols(1, 2) = 10 'Dimensiunea coloanei (10)
arrCols(1, 3) = 0 'Modalitatea de aliniere pe orizontala(0=Stanga, 1=Centru, 2=Dreapta)
arrCols(1, 4) = 0 'Modalitatea de aliniere pe verticala(0=Sus, 1=Centru, 3=Jos)

'Setari COL nr.2
arrCols(2, 1) = 3
arrCols(2, 2) = 30
arrCols(2, 3) = 1
arrCols(2, 4) = 1

'Setari COL nr.3
arrCols(3, 1) = 4
arrCols(3, 2) = 20
arrCols(3, 3) = 2
arrCols(3, 4) = 3

For Each tbl In ActiveDocument.Tables
'verifica numarul de coloane al tabelului sa fie cel putin cat al matricei de coloane
If tbl.Columns.Count >= UBound(arrCols, 1) Then
For i = 1 To UBound(arrCols, 1)
tbl.Columns(arrCols(i, 1)).PreferredWidthType = wdPreferredWidthPercent 'Seteaza tipul modificari - procent
tbl.Columns(arrCols(i, 1)).PreferredWidth = arrCols(i, 2) 'Modifica cu dimensiunea ceruta

'Urmeaza partea de "aliniere"
tbl.Columns(arrCols(i, 1)).Select
Selection.ParagraphFormat.Alignment = arrCols(i, 3) 'Modalitatea de aliniere pe orizontala
Selection.Cells.VerticalAlignment = arrCols(i, 4) 'Modalitatea de aliniere pe verticala
Next i
Else
Debug.Print "Tabelul nu are dimensiunea corecta" & tbl.Columns.Count ' Asta se vede numai in Immediate window
End If
Next tbl
End Sub