在处理word时,经常会需要调整表格样式,工作比较繁琐,收集整理了自动设置表格格式的宏,代码如下:
Sub 设置表格格式()
'功能:光标在表格中处理当前表格;否则处理所有表格!
Application.ScreenUpdating = False '关闭屏幕刷新
Application.DisplayAlerts = False '关闭提示
On Error Resume Next '忽略错误
'***************************************************************************
Dim mytable As Table, i As Long
If Selection.Information(wdWithInTable) = True Then i = 1
For Each mytable In ActiveDocument.Tables
If i = 1 Then Set mytable = Selection.Tables(1)
With mytable
'取消底色
.Style = "网格型"
.Shading.ForegroundPatternColor = wdColorAutomatic
.Shading.BackgroundPatternColor = wdColorAutomatic
.Shading.Texture = wdTextureNone '无底纹
.Range.HighlightColorIndex = wdNoHighlight '去除高亮
'单元格边距
.TopPadding = PixelsToPoints(0, True) '设置上边距为0
.BottomPadding = PixelsToPoints(0, True) '设置下边距为0
.LeftPadding = PixelsToPoints(0, True) '设置左边距为0
.RightPadding = PixelsToPoints(0, True) '设置右边距为0
.Spacing = PixelsToPoints(0, True) '允许单元格间距为0
.AllowPageBreaks = True '允许断页
'.AllowAutoFit = True '允许自动重调尺寸
With .Rows
.Alignment = wdAlignRowCenter '表水平居中
.WrapAroundText = False '取消文字环绕
.AllowBreakAcrossPages = False '不允许行断页
.HeightRule = wdRowHeightExactly '行高设为最小值
.Height = CentimetersToPoints(0) '上面缩进量为0
.LeftIndent = CentimetersToPoints(0) '左面缩进量为0
End With
With .Range
With .Font '字体格式
.Name = "仿宋"
.Name = "仿宋"
.Size = 12
.Bold = False
End With
.Cells.VerticalAlignment = wdCellAlignVerticalCenter '单元格垂直居中
With .ParagraphFormat '段落格式
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.FirstLineIndent = CentimetersToPoints(0) '取消首行缩进
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphCenter '单元格水平居中
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0 '取消首行缩进
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
.CollapsedByDefault = False
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = True
.FarEastLineBreakControl = True
.WordWrap = True
.HangingPunctuation = True
.HalfWidthPunctuationOnTopOfLine = False '将活动文档第一段的行首标点符号改为半角字符
.AddSpaceBetweenFarEastAndAlpha = False '设置为自动添加指定段落的日语和西文文字之间的空格
.AddSpaceBetweenFarEastAndDigit = False
.BaseLineAlignment = wdBaselineAlignAuto
End With
End With
'设置首行格式
With .Rows.First
.HeadingFormat = wdToggle '重复标题行
.Range.Font.Bold = wdToggle
.Shading.ForegroundPatternColor = wdColorAutomatic
.Shading.BackgroundPatternColor = -570359809 '-603923969
End With
'自动调整表格
.Columns.PreferredWidthType = wdPreferredWidthAuto
.AutoFitBehavior (wdAutoFitContent) '根据内容调整表格
.AutoFitBehavior (wdAutoFitWindow) '根据窗口调整表格
End With
If i = 1 Then Exit For
Next
'***************************************************************************
Err.Clear: On Error GoTo 0 '恢复错误捕捉
Application.DisplayAlerts = True '开启提示
Application.ScreenUpdating = True '开启屏幕刷新
End Sub
设置前的样式:
设置后的样式