I'm trying to set AutoFitBehavior of a Word table to fit both content and window - clicking 'AutoFit Contents' button, then 'AutoFit Window' gives the result I want to get. The problem is, when I do this using VBA, the formatting is different. Interestingly enough, when I run the macro Step By Step (F8) it gives expected result (same as UI).
Here's my code:
Documents(1).Activate
With ActiveDocument.Tables(2)
.AllowAutoFit = True
.AutoFitBehavior 1
.AutoFitBehavior 2
End With
As you can see, it's pretty simple - I can't find any reason for it to work incorrectly.
Also I don't think the issue is related to using 'ActiveDocument' property, as in the full macro this code is executed directly on a newly created, named document, so I'm sure it's addressing a correct table in a correct file.
I am aware that I can set column widths with PreferredWidth property, but it would be much simpler to use AutoFit, as I don't always know what length will my data have.
Is there a way to make this method work as when called from UI?
Edit: As per Cindy Meister's request, I'm adding snippet from actual code I'm using:
Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Add(strPath)
With wordDoc
.Tables.Add Range:=wordDoc.Bookmarks("tableBookmark").Range, NumRows:=licenceRows, NumColumns:=3
'[omitted: populating the table]
.Tables(1).Split(splitRow)
With .Tables(2)
.Range.Collapse Direction:=0
.Range.InsertBreak Type:=7
.AllowAutoFit = True
.AutoFitBehavior 1
.AutoFitBehavior 2
End With
End With
It's called from an Excel macro I'm using to create a report file from template. I'm using Office 2013.
Also I've noticed another thing today: when I set wordApp.Visible = True
, scroll to the table and literally look at the method working - it formats correctly. It's like Word application won't use this method correctly, until it has to show you every single step (as with step-by-step run).
Thanks to Cindy's answer and following comment I realised my mistake - I thought Auto Fit would make columns fit to any text, including text with line-breaking characters like spaces. Comes out it doesn't work that way.
In the end, to format the table as I wanted (window-wide table, columns fit to content) I used the following code:
'Table should fit the page and fit the contents
Sub TestFormatTableStructure()
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim tbl1 As Word.Table, tbl2 As Word.Table
On Error GoTo ErrHandler
Set wordApp = New Word.Application
Set wordDoc = wordApp.Documents.Add
wordApp.ScreenUpdating = False
With wordDoc
Set tbl1 = .Tables.Add(Range:=wordDoc.Paragraphs.Last.Range, _
NumRows:=6, NumColumns:=3, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent) 'autofit content
With tbl1
'[omitted: populating the table]
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
End With
Set tbl2 = tbl1.Split(4)
'dont have to set formatting again for second table, its inherited
With tbl2
'[do things]
End With
End With
ErrHandler:
wordApp.Visible = True
wordApp.ScreenUpdating = True
Set tbl1 = Nothing
Set tbl2 = Nothing
Set rngtlb = Nothing
Set wordDoc = Nothing
Set wordApp = Nothing
End Sub