word文档中宏的使用(使用VBA批量修改表格样式)

编程入门 行业动态 更新时间:2024-10-18 22:29:40

word文档中宏的使用(使用VBA<a href=https://www.elefans.com/category/jswz/34/1770428.html style=批量修改表格样式)"/>

word文档中宏的使用(使用VBA批量修改表格样式)

1. 找到工具栏中“视图-》宏


2. 选择“查看宏

3. 创建/编辑宏

4. 修改“表格背景和设定字体大小”代码

如图:

代码块: 

Sub 修改表格字体()
'
' 修改表格字体 宏
'
'
For i = 1 To ActiveDocument.Tables.CountDim t As TableSet t = ActiveDocument.Tables(i)With t'断开活动文档的第1个表格的域的链接.Range.Fields.Unlink'关于字体的各项设置,可以通过录制宏得到
'    Range.Font 属性
'    返回或设置 Font 对象,该对象代表指定对象的字符格式With .Range.Font.NameFarEast = "宋体" '中文字体.NameAscii = "宋体" '西文字体.Size = 10.5 '字号End WithWith t.Rows(1).Shading.BackgroundPatternColor = wdColorWhite '设置第一行的背景颜色为白色End WithEnd With
Next i
End Sub

5. “运行”,运行中有错误会提示

文档有些较大的,运行时间教长,需要耐心等待~

其他代码可参考

1)设置表格的字体

Sub 设置表格的字体()Dim t As TableSet t = ActiveDocument.Tables(1)With t'断开活动文档的第1个表格的域的链接.Range.Fields.Unlink'关于字体的各项设置,可以通过录制宏得到
'    Range.Font 属性
'    返回或设置 Font 对象,该对象代表指定对象的字符格式With .Range.Font.NameFarEast = "宋体" '中文字体.NameAscii = "Times New Roman" '西文字体.Bold = False '字形 不加粗.Italic = False '字形 不是斜体.Size = 9 '字号.ColorIndex = wdBlack '字体颜色.Underline = wdUnderlineNone '下划线 无.UnderlineColor = wdColorBlack '下划线 颜色.EmphasisMark = wdEmphasisMarkNone '着重号.StrikeThrough = False '删除线.DoubleStrikeThrough = False '双删除线.Superscript = False '字体格式 上标.Subscript = False '字体格式 下标.SmallCaps = False '小型大写字母 字母的形状和大写字母相同但尺寸较小.AllCaps = False '全部大写字母 如果为true 字母全部大写.Hidden = False '隐藏 如果设置为true,打印的时候看不到End WithEnd With
End Sub

2)设置表格的局部的字体

Sub 设置表格的第一行的字体()'第一行的字体加粗Dim t As TableSet t = ActiveDocument.Tables(1)With t.Rows(1).Range.Font.Bold = True '字形 加粗.Size = 10.5 '字号End With
End Sub

3)设置表格边框

注:本部分样式代码原文地址:使用宏设置word中的表格样式_Sheyueyu的博客-CSDN博客

Sub SetTableBorders()'遍历所有表格For Each tbl In ActiveDocument.Tables'设置表格顶部和底部边框为1.5tbl.Borders(wdBorderTop).LineStyle = wdLineStyleSingletbl.Borders(wdBorderTop).LineWidth = wdLineWidth150pttbl.Borders(wdBorderBottom).LineStyle = wdLineStyleSingletbl.Borders(wdBorderBottom).LineWidth = wdLineWidth150pt'设置第二行的上下边框为0.75tbl.Rows(2).Borders(wdBorderTop).LineStyle = wdLineStyleSingletbl.Rows(2).Borders(wdBorderTop).LineWidth = wdLineWidth075pttbl.Rows(2).Borders(wdBorderBottom).LineStyle = wdLineStyleSingletbl.Rows(2).Borders(wdBorderBottom).LineWidth = wdLineWidth075pt'遍历表格中除了第一行和最后一行以外的其余行For i = 3 To tbl.Rows.Count - 1tbl.Rows(i).Borders(wdBorderTop).LineStyle = wdLineStyleNonetbl.Rows(i).Borders(wdBorderBottom).LineStyle = wdLineStyleNoneNext i'设置第一行下的边框为0.75tbl.Rows(1).Borders(wdBorderBottom).LineStyle = wdLineStyleSingletbl.Rows(1).Borders(wdBorderBottom).LineWidth = wdLineWidth075pt'隐藏表格的列边框For j = 1 To tbl.Columns.Counttbl.Columns(j).Borders(wdBorderLeft).LineStyle = wdLineStyleNonetbl.Columns(j).Borders(wdBorderRight).LineStyle = wdLineStyleNoneNext jNext tbl
End Sub

更多推荐

word文档中宏的使用(使用VBA批量修改表格样式)

本文发布于:2024-02-26 19:32:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1703642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批量   样式   表格   文档   word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!