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 / February 2008

Tip: Looking for answers? Try searching our database.

VBA: Read current Table's Autofit

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darren Hill - 14 Feb 2008 11:15 GMT
[Excel 2007]
I have a macro that performs operations on a table, based on its current
autofit style, so I need to be able to distnguish between the following
four conditions:
Table currently set to:
* Autofit to Window
* Autofit to Contents
* Fixed Width (all columns equal width)
* Fixed Width (columns have different width)

Actually, differentiating between the last two isn't that important, but
I do need to differentiate between the first three.

I tried using autofitbehavior, but either I'm using it wrong or it can
only be used to set autofit, not read it.

So I scaled back my goal and tried to distinguish between autofit
(either type) and non-autofit, with the following code, but that doesn't
work reliably either.

    With tblTable
        If .AllowAutoFit = True Then
            AutoFit = wdAutoFitWindow
        Else
            AutoFit = wdAutoFitFixed
        End If
    End With

Can anyone help out?

Thanks,
Darren
Darren Hill - 15 Feb 2008 16:10 GMT
So, does no-one know how to identify a table's autofit setting in VBA?

> [Excel 2007]
> I have a macro that performs operations on a table, based on its current
[quoted text clipped - 28 lines]
> Thanks,
> Darren
Isa Muqattash - 15 Feb 2008 20:27 GMT
Hi,

I am no expert, but check out this documentation. Hope it helps.

Determines how Microsoft Word resizes a table when the AutoFit feature is
used. Word can resize the table based on the content of the table cells or
the width of the document window. You can also use this method to turn off
AutoFit so that the table size is fixed, regardless of cell contents or
window width.

expression.AutoFitBehavior(Behavior)
expression    Required. An expression that returns a Table object.

Behavior   Required WdAutoFitBehavior. How Word resizes the specified table
with the AutoFit feature is used.

WdAutoFitBehavior can be one of these WdAutoFitBehavior constants.
wdAutoFitContent
wdAutoFitWindow
wdAutoFitFixed

Remarks
Setting the AutoFit behavior to wdAutoFitContent or wdAutoFitWindow sets the
AllowAutoFit property to True if it's currently False. Likewise, setting the
AutoFit behavior to wdAutoFitFixed sets the AllowAutoFit property to False if
it's currently True.

Example
This example sets the AutoFit behavior for the first table in the active
document to automatically resize based on the width of the document window.

ActiveDocument.Tables(1).AutoFitBehavior _
   wdAutoFitWindow
Isa Muqattash - 15 Feb 2008 20:29 GMT
Hi,

I am no expert, but check out the documentation on the table object. Hope it
helps.

Determines how Microsoft Word resizes a table when the AutoFit feature is
used. Word can resize the table based on the content of the table cells or
the width of the document window. You can also use this method to turn off
AutoFit so that the table size is fixed, regardless of cell contents or
window width.

expression.AutoFitBehavior(Behavior)
expression    Required. An expression that returns a Table object.

Behavior   Required WdAutoFitBehavior. How Word resizes the specified table
with the AutoFit feature is used.

WdAutoFitBehavior can be one of these WdAutoFitBehavior constants.
wdAutoFitContent
wdAutoFitWindow
wdAutoFitFixed

Remarks
Setting the AutoFit behavior to wdAutoFitContent or wdAutoFitWindow sets the
AllowAutoFit property to True if it's currently False. Likewise, setting the
AutoFit behavior to wdAutoFitFixed sets the AllowAutoFit property to False if
it's currently True.

Example
This example sets the AutoFit behavior for the first table in the active
document to automatically resize based on the width of the document window.

ActiveDocument.Tables(1).AutoFitBehavior _
   wdAutoFitWindow
Darren Hill - 16 Feb 2008 08:19 GMT
Thanks for the response. I tried using autofitbehavior, but was only
able to use it to set a table's status - not to read what it was.
Maybe I'm not understanding the syntax.

If I have a table declared

Set tbl = selection.Tables(1)

and I have a variable,
 Dim Autofit as WdAutoFitBehavior

how do I get the table's current autofit status into that variable?

I tried
Autofit = tbl.autofitbehavior
but that generates an error - autofitbehavior needs a parameter.

Thanks,
Darren

> Hi,
>
[quoted text clipped - 30 lines]
> ActiveDocument.Tables(1).AutoFitBehavior _
>     wdAutoFitWindow
Tony Strazzeri - 17 Feb 2008 01:05 GMT
Hi Darren,

Had a play with this and notice that the preferredwidth seems to
change with the AutoFitBehavior

   'setting                      PreferredWidth;
   '==============    ==============
   'wdAutoFitWindow     100
   'wdAutoFitContent     0
   'wdAutoFitFixed        0 initially
   '                               if any column width is changed
this becomes 9999999

Hope this helps.

Cheers
TonyS.
Darren Hill - 17 Feb 2008 15:36 GMT
Outstanding! I'd given up hope of finding a solution, but with that
help, I think I've cracked it.
The preferred width, in combination with table.allowautofit, gives me
the settings I need.

Thank you, Tony.

In case you're interested, here's the function I'm using:

Function TablesAutofit(tbl As Table) As String
'AUTOFIT_WINDOW As String = "To Window"
'AUTOFIT_CONTENTS As String = "To Contents"
'AUTOFIT_FIXED As String = "Fixed Width"
'    - no autofit, all columns equal width
'AUTOFIT_DISABLED As String = "Disabled"
'    - no autofit, columns of variable width

' this function examines a table,
' determines preferred width type
' then checks preferred width
' if preferred width <100 and allowautofit = true: autofit to contents
' if preferred width = 100% and allowautofit = true, autofit to window
' if allowautofit = false and columns equal, fixed width.
' otherwise disabled.

    Dim TypePrefWidth As WdPreferredWidthType
    Dim i As Integer
    Dim FirstColWidth As Long, NewColWidth As Long
    Dim bAllColumnsEqual As Boolean

    TypePrefWidth = tbl.PreferredWidthType
    tbl.PreferredWidthType = wdPreferredWidthPercent
    With tbl
        If .PreferredWidth < 100 And .AllowAutoFit = True Then
            TablesAutofit = AUTOFIT_CONTENTS
        ElseIf .PreferredWidth >= 100 And .AllowAutoFit = True Then
            TablesAutofit = AUTOFIT_WINDOW
        Else
            bAllColumnsEqual = True
            If .Columns.Count > 1 Then
                FirstColWidth = .Columns(1).Width
                For i = 2 To .Columns.Count
                    NewColWidth = .Columns(i).Width
                    If NewColWidth <> FirstColWidth Then
                        bAllColumnsEqual = False
                        Exit For
                    End If
                Next i
            End If

            If bAllColumnsEqual = True Then
                TablesAutofit = AUTOFIT_FIXED
            Else
                TablesAutofit = AUTOFIT_DISABLED
            End If
        End If
    End With
    ' return table to the preferredwidthtype it started with
    tbl.PreferredWidthType = TypePrefWidth
End Function

Darren

> Hi Darren,
>
[quoted text clipped - 13 lines]
> Cheers
> TonyS.
 
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.