OK. I changed the font code to use the ".range". That now works. Is there some way I should have known that or been able to figure it out?
I deleted the 'Selection.Tables(1)', but I still get the error.
I've posted the entire macro below. It has the right variables declared. I made a bunch of changes to the way the options are selected and reported, but the core code is the same.
Code:
Sub MyTableSettings()
Const MyName = "MyTableSettings"
Dim SettingBreakOld, SettingAutoFitOld, SettingFontSw, SettingFontOld, _
SettingHdrRowSw, SettingHdrRowOld, SettingHdrBrdrOffSw, SettingSelectTableSw _
As Boolean 'Setting variables
Dim Msg, MsgAutoFit, MsgBreak, MsgHdrRow, MsgFont, MsgBorder As String
'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
'Get user options
If vbYes = MsgBox("Take all defaults?", vbYesNo, MyName) Then
SettingHdrRowSw = True
SettingHdrBrdrOffSw = True
SettingFontSw = True
SettingSelectTableSw = True
Else
SettingHdrRowSw = (vbYes = MsgBox("Set row 1 as header?", vbYesNo, MyName))
SettingHdrBrdrOffSw = (vbYes = MsgBox("Header row borders off?", vbYesNo, MyName))
SettingFontSw = (vbYes = MsgBox("Set font to Calibri?", vbYesNo, MyName))
SettingSelectTableSw = (vbYes = MsgBox("Leave table selected?", vbYesNo, MyName))
End If
With Selection.Tables(1)
'Save the old settings
SettingBreakOld = .Rows.AllowBreakAcrossPages
SettingAutoFitOld = .AllowAutoFit
SettingHdrRowOld = .Rows(1).HeadingFormat
SettingFontOld = .Range.Font.Name
'Set the rest of the new settings
.Rows.AllowBreakAcrossPages = False 'Do not allow rows to break across pages
MsgBreak = "Break = " & .Rows.AllowBreakAcrossPages & " (was " & SettingBreakOld & ")" & vbCrLf
.AllowAutoFit = False 'Do not autofit columns
MsgAutoFit = "Autofit = " & .AllowAutoFit & " (was " & SettingAutoFitOld & ")" & vbCrLf
.Rows(1).HeadingFormat = SettingHdrRowSw 'Set header row on/off
MsgHdrRow = "Header row = " & .Rows(1).HeadingFormat & " (was " & SettingHdrRowOld & ")" & vbCrLf
If SettingFontSw Then 'Set font?
.Range.Font.Name = "Calibri"
MsgFont = "Font = " & .Range.Font.Name & " (was " & SettingFontOld & ")" & vbCrLf
Else
MsgFont = ""
End If
If SettingHdrBrdrOffSw Then 'If no header row borders, turn all but bottom off
With .Rows(1)
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
' .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
' .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
End With
MsgBorder = "Header row borders off"
Else
MsgBorder = "Header row borders unchanged"
End If
If SettingSelectTableSw Then Selection.Tables(1).Select 'Leave table selected?
'Report the results
Msg = MsgAutoFit & MsgBreak & MsgHdrRow & MsgFont & MsgBorder
MsgBox Msg, vbOKOnly, MyName
End With
End Sub