VBA: Read current Table's Autofit

D

Darren Hill

[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
 
I

Isa Muqattash

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
 
I

Isa Muqattash

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
 
D

Darren Hill

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
 
T

Tony Strazzeri

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.
 
D

Darren Hill

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
 

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