Autofit to content then to window issues

时间:2019-04-17 00:23:37

标签: vba ms-word

I am trying to write a script to autofit tables first to content, then to window n Word 2010, as long as they are at least a certain width.

When I manually autofit to content then to window, the tables look nice and evenly spaced, however if I ONLY autofit to window they tend to have really wide first columns and squished up other columns.

code:

Sub AutoFitTables()

  If ActiveDocument.Tables.Count > 0 Then
    Dim objTable As Object

    Application.Browser.Target = wdBrowseTable
    For Each objTable In ActiveDocument.Tables
      ' Check the table width 
      If objTable.Columns.Width > 11520 Then
        objTable.AutoFitBehavior (wdAutoFitContent)
        ' Again only fit tables above a certain width
        If objTable.Columns.Width > 11520 Then
           objTable.AutoFitBehavior (wdAutoFitWindow)
        End If
      End If
    Next
  End If

End Sub

issue:

The result is the same as when I manually ONLY auto fit to window. It's like it ignores the autofit to contents first

Is there something that I'm missing here? Or does anyone have suggestion of how to achieve the same effect as manually fitting to content, then to window?

edit:

It also seems to be ignoring the width constraint. I believe the table with is returned in Twips, so there should be only 11907 along the short edge and 16840 along the long edge, but not matter how large I set the cutoff (eg objTable.Columns.Width > 20000) it still autofits all tables. So if anyone knows why that is happening that would be helpful too

0 个答案:

没有答案