View Single Post
 
Old 01-28-2012, 03:41 PM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline Windows XP Office 2007
Competent Performer
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default

I think I got it. Here's the macro. It changes three settings:
  • It turns off the Break Across Pages setting (for rows).
  • It turns off the Auto Fit setting (for columns).
  • It set row 1 as a header row if the user says "Yes".
Here's the code. Comments appreciated.

Code:
'===========================================================================
'                        My Table Settings
' Correct the table settings to what Word should have made them.
' 07/23/10  Recorded and modified.
' 01/28/12  Add code for header row
'===========================================================================
Sub MyTableSettings()
Const MyName = "MyTableSettings"
Dim SettingBreakOld                 'Old setting
Dim SettingAutoFitOld               'Old setting
Dim SettingHeaderRow As Boolean     'True = set first row as header
Dim SettingHeaderRowOld As Boolean  'Old setting
Dim Msg
'Abort if the cursor is not in a table
If Selection.Information(wdWithInTable) <> True Then
  MsgBox "The cursor is not in a table", vbOKOnly, MyName
  Exit Sub
End If
 
'Ask if they want a header row?
Select Case MsgBox("Set row 1 as header?", vbYesNoCancel, MyName)
  Case vbYes
    SettingHeaderRow = True
  Case vbNo
    SettingHeaderRow = False
  Case vbCancel
    MsgBox "Aborted", vbOKOnly, MyName
    Exit Sub
End Select
'Save old settings
SettingBreakOld = Selection.Rows.AllowBreakAcrossPages
If SettingBreakOld = -1 Then   '-1 = On, 0 = Off
  SettingBreakOld = "On"
ElseIf SettingBreakOld = 0 Then
  SettingBreakOld = "Off"
End If
SettingAutoFitOld = Selection.Tables(1).AllowAutoFit
If SettingAutoFitOld = "True" Then   'True = On, False = Off
  SettingAutoFitOld = "On"
ElseIf SettingAutoFitOld = "False" Then
  SettingAutoFitOld = "Off"
End If
SettingHeaderRowOld = Selection.Tables(1).Rows(1).HeadingFormat
'Set the new ones
Selection.Tables(1).Select                                    'Select the entire table
Selection.Rows.AllowBreakAcrossPages = False                  'Stop rows from breaking
Selection.Tables(1).AllowAutoFit = False                      'Stop auto-resizing
Selection.Tables(1).Rows(1).HeadingFormat = SettingHeaderRow  'Set the heading setting
'Report the results
Msg = "Autofit = Off (was " & SettingAutoFitOld & "), " & vbCrLf & _
      "Break = Off (was " & SettingBreakOld & "), " & vbCrLf & _
      "Header row = " & SettingHeaderRow & " (was " & SettingHeaderRowOld & ")"
MsgBox Msg, vbOKOnly, MyName
End Sub
Reply With Quote