More about tables

J

Jeff Hall

Is there another way to get the width of a table (in points) other than to
iterate row-by-row, adding-up the cell widths and then taking the widest
row?
 
H

Helmut Weber

Hi Jeff,
one way may to be this one:
Sub WidthofTable()
Dim x1 As Single
Dim x2 As Single
' 5 = wdHorizontalPositionRelativeToPage
With ActiveDocument.Tables(1)
.Cell(1, 1).Select
x1 = Selection.Information(5)
x2 = 0
For i = 1 To 5 ' replace by rows.count
.Cell(i, .Rows(i).Cells.Count).Select
Selection.Collapse direction:=wdCollapseEnd
If Selection.Information(5) > x2 Then
x2 = Selection.Information(5)
End If
Next
End With
MsgBox x2 - x1
End Sub
Maybe not too fast either, and not too elegant.
Not checked with vertically merged cells
and other gotchas.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word XP, NT 4.0
 
J

Jeff Hall

Oh dear... your solution works BUT....

I have a subroutine that calls your routine as a function...
The compiler won't allow the table to be passed!!!!!!!
It reports...

"byref argument type mismatch" (aTable)

Grrrrrrrrr......!

Sub GetWidths()
dim aTable as Table
dim TableWidths()
Dim i
i=-1
For Each aTable In ActiveDocument.Tables
i = i + 1
Redim Preserve TableWidths(i)
TableWidths(i) = WidthofTable(aTable)
next
end sub

Function WidthofTable(aTable As Table)
Dim x1 As Single
Dim x2 As Single
Const hPos = wdHorizontalPositionRelativeToPage
With aTable
.Cell(1, 1).Select
x1 = Selection.Information(hPos)
x2 = 0
For i = 1 To .Rows.Count
.Cell(i, .Rows(i).Cells.Count).Select
Selection.Collapse direction:=wdCollapseEnd
If Selection.Information(hPos) > x2 Then
x2 = Selection.Information(hPos)
End If
Next
End With
WidthofTable = x2 - x1
End Function
 
J

Jeff Hall

Oh It's OK I'm just a dunderhead... a coding error in the actual code from
which this extract was created.
 
A

Andi Mayer

Use your counter "i" to reference to the table in your function

like

TableWidths(i) = WidthofTable(i)
......
Function WidthofTable(aTable As integer)
......
With ActiveDocument.Tables(aTable)
......

MW
 
J

Jeff Hall

After much scratching of heads...

Your routine does not work with tables that have vertically merged cells!
If you try to access "Rows(i)" information in such a table, it makes Word
CRASH - no soft option here I'm afraid!!

Here is a function that has stripped me of several months of life expectancy
to develop......

I'll leave it to you to work out where the email program has forced a line
break.....

Function WidthofTable(myTable As Table)
'Can't do rows on tables with vertically merged cells
Dim x1, x2
Dim r1, r2
Dim i, j
x1 = 0: x2 = 0
r1 = 0: r2 = 0
i = 0
WidthofTable = 0
j = myTable.Range.Cells.count
myTable.Cell(1, 1).Select
With Selection
.Collapse direction:=wdCollapseStart
x1 = .Information(wdHorizontalPositionRelativeToPage)
r1 = .Information(wdStartOfRangeRowNumber)
For i = 1 To j
myTable.Range.Cells(i).Select
If i < j Then
r2 = .Information(wdStartOfRangeRowNumber)
If r2 > r1 Then
r1 = r2
myTable.Range.Cells(i - 1).Select
.Collapse direction:=wdCollapseEnd
If .Information(wdHorizontalPositionRelativeToPage) > x2
Then
x2 = .Information(wdHorizontalPositionRelativeToPage)
End If
myTable.Range.Cells(i).Select
End If
Else
ActiveDocument.Range(Start:=.Range.End - 1, End:=.Range.End -
1).Select
If .Information(wdHorizontalPositionRelativeToPage) > x2 Then
x2 = .Information(wdHorizontalPositionRelativeToPage)
End If
End If
Next
End With
WidthofTable = x2 - x1
'MsgBox (x2 - x1)
End Function


--------------------------------------------------------------------
Jeff Hall MSc MRICS
Director, Eon Commerce Ltd.
http://www.eon-commerce.com

Software available for you to evaluate before buying...
EasyHTML/Help CHM file Editor for MS Word
http://www.easyhtmlhelp.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top