vba提取word表格样式:解析WordVBA代码生成三线表样式
人有悔意,请天怜之。人有悔意,请天怜之。人有悔意,请天怜之。
使用表格样式进行批量调整表格格式如果不知道表格样式的,可以去看一下解析Word——Word法宝「样式」之「表格样式」。
如果你Word文章中有许多表格的格式要调整成一致,建议你使用表格样式进行批量调整,而不是用格式刷去刷。比如你的文章中有很多表格,想调整为统一格式的三线表,你可以建立一种三线表式去调整。如下图所示:
使用【表格样式】 【全选表格】进行批量调整表格格式
本文将讲述如何使用VBA代码建立三线表样式
建立三线表表格样式代码
Sub 三线表表格样式()
b = False
Set MyDoc = ActiveDocument
MyTableName = "三线表"
For Each MyStyle In MyDoc.Styles
If MyStyle.NameLocal = "三线表" Then
Application.StatusBar = "该样式已存在!"
Exit Sub
Else
b = False
End If
Next
If b = False Then
ActiveDocument.Styles.Add Name:=MyTableName, Type:=wdStyleTypeTable
' 整体表格字体设置
With ActiveDocument.Styles(MyTableName).Font
.NameFarEast = "宋体"
.NameAscii = "Times New Roman"
.NameOther = "Times New Roman"
.Name = "Times New Roman"
.Size = 10.5
End With
' 整体表格段落设置
With ActiveDocument.Styles(MyTableName).ParagraphFormat
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphCenter
.SpaceAfter = 0
End With
With ActiveDocument.Styles(MyTableName).Table
.Alignment = wdAlignRowCenter
.AllowPageBreaks = True
.AllowBreakAcrossPage = True
End With
' 整体表格边框线设置
With ActiveDocument.Styles(MyTableName).Table.Borders
.OutsideLineStyle = False
.InsideLineStyle = False
End With
With ActiveDocument.Styles(MyTableName).Table.Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
End With
' 标题行边框设置
With ActiveDocument.Styles(MyTableName).Table.Condition(wdFirstRow)
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
End With
' 标题行段落格式设置
With ActiveDocument.Styles(MyTableName).Table.Condition(wdFirstRow). _
ParagraphFormat
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphCenter
End With
' 标题行字体格式设置
With ActiveDocument.Styles(MyTableName).Table.Condition(wdFirstRow).Font
.NameFarEast = "黑体"
.NameAscii = "Times New Roman"
.NameOther = "Times New Roman"
.Name = "Times New Roman"
.Size = 10.5
End With
' 汇总行边框线设置
With ActiveDocument.Styles(MyTableName).Table.Condition(wdLastRow).Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
End With
' 汇总行段落格式设置
With ActiveDocument.Styles(MyTableName).Table.Condition(wdLastRow).ParagraphFormat
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphCenter
End With
' 汇总行字体格式设置
With ActiveDocument.Styles(MyTableName).Table.Condition(wdLastRow).Font
.NameFarEast = "宋体"
.NameAscii = "Times New Roman"
.NameOther = "Times New Roman"
.Name = "Times New Roman"
.Size = 10.5
End With
End If
Application.StatusBar = "三线表表格样式新建完毕!"
End Sub
- 打开Word后,按下Alt F11组合键,打开VBE窗口,将代码复制进VBE窗口中。
将代码复制到VBE窗口中
- 单击执行按钮,执行程序。程序执行完毕以后,如果成功建立样式,则在Word的状态栏会显示提示信息。
执行代码建立三线表样式
- 选中要调整的表格,为这些表格应用新建的【三线表】样式,即可批量为这些表格调整格式。
免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com