JWS315 was telling us:
JWS315 nous racontait que :
I need to determine the width of tables prior to copy them into a 2
column document using VBA. I looked at the columns width property
but not all the tables have simple columns, some have nested or
merged columns.
Is there an easy way to detemine the width of any table regardless of
the columns?
There are two problems, one for which I can offer a solution and the other I
cannot...
First, if the table has columns that have user-set width, .PreferredWidth
will return 999999. This can be detected and we can get around that, as in
my example below.
Second, if you encounter the first case and then have to use the workaround,
if there are merged cells in the table, it is possible that an error be
generated.
'_______________________________________
Option Explicit
'_______________________________________
Sub GetNormalWidth()
Dim TableWidthType As Long
Dim TableWidth As Single
Dim myTable As Table
Set myTable = ActiveDocument.Tables(1)
With myTable
'Save current setting
TableWidthType = .PreferredWidthType
'Apply width type in absolute, not relative percentage
.PreferredWidthType = wdPreferredWidthPoints
'Get width
If .PreferredWidth = 9999999 Then
TableWidth = CSng(GetSpecialWidth(myTable))
If TableWidth = 0 Then Exit Sub
Else
TableWidth = .PreferredWidth
End If
'Reset width type
.PreferredWidthType = TableWidthType
'Limit to 2 digits after deciaml point
TableWidth = Format(PointsToInches(TableWidth), "#0.00")
MsgBox TableWidth & " inches wide"
End With
End Sub
'_______________________________________
'_______________________________________
Private Function GetSpecialWidth(myTable As Table) As Integer
Dim TableWidth As Integer
Dim iCount As Integer
On Error GoTo DealError
With myTable
For iCount = 1 To .Columns.Count
TableWidth = TableWidth + .Cell(1, iCount).Width
Next iCount
End With
GetSpecialWidth = TableWidth
Exit Function
DealError:
Err.Clear
MsgBox "The table width cannot be calculated, probably because there " _
& "are merged cells in the table.", vbCritical, _
"Cannot compute width"
End Function
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org