Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

How to determine width of tables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JWS315 - 26 Oct 2005 18:22 GMT
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
Anne Troy - 26 Oct 2005 21:06 GMT
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

>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
[quoted text clipped - 6 lines]
> Thanks!
> Jerry
JWS315 - 26 Oct 2005 22:53 GMT
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

> Recorded a macro to set the table width:
>     Selection.Tables(1).Select
[quoted text clipped - 17 lines]
> > Thanks!
> > Jerry
Jean-Guy Marcil - 26 Oct 2005 22:14 GMT
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
[quoted text clipped - 3 lines]
> 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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

JWS315 - 26 Oct 2005 23:02 GMT
Just what I was encoutering - Thanks for the code workaround!!

Thanks - Jerry

> JWS315 was telling us:
> JWS315 nous racontait que :
[quoted text clipped - 77 lines]
> End Function
> '_______________________________________
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.