How to determine width of tables

J

JWS315

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?

Thanks!
Jerry
 
A

Anne Troy

Recorded a macro to set the table width:
Selection.Tables(1).Select
Selection.Tables(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables(1).PreferredWidth = InchesToPoints(5)
Maybe that'll help you get what you need? Realizing of course that you'd
probably write it much more efficiently.
Anyway, just wanted to show you that tables have a Width too.
************
Anne Troy
www.OfficeArticles.com
 
J

Jean-Guy Marcil

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
 
J

JWS315

Anne,

I tried the same code, however if you add the line:
msgbox Selection.Tables(1).PreferredWidth

it returns the value 999999 for any table that has merged cells.

I am able to set the width OK but avnnot find a way to return the value
properly - any thoughts?

Thanks - jerry
 

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