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

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


2. 选择“查看宏

3. 创建/编辑宏

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

如图:

代码块: 

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

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

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

其他代码可参考

1)设置表格的字体

Sub 设置表格的字体()
  Dim t As Table
  Set 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 With
  End With
End Sub

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

Sub 设置表格的第一行的字体()
  '第一行的字体加粗
  Dim t As Table
  Set 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.5
        tbl.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
        tbl.Borders(wdBorderTop).LineWidth = wdLineWidth150pt
        tbl.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        tbl.Borders(wdBorderBottom).LineWidth = wdLineWidth150pt
        
        '设置第二行的上下边框为0.75
        tbl.Rows(2).Borders(wdBorderTop).LineStyle = wdLineStyleSingle
        tbl.Rows(2).Borders(wdBorderTop).LineWidth = wdLineWidth075pt
        tbl.Rows(2).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        tbl.Rows(2).Borders(wdBorderBottom).LineWidth = wdLineWidth075pt
        
        '遍历表格中除了第一行和最后一行以外的其余行
        For i = 3 To tbl.Rows.Count - 1
            tbl.Rows(i).Borders(wdBorderTop).LineStyle = wdLineStyleNone
            tbl.Rows(i).Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        Next i
        
        '设置第一行下的边框为0.75
        tbl.Rows(1).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        tbl.Rows(1).Borders(wdBorderBottom).LineWidth = wdLineWidth075pt
        
        '隐藏表格的列边框
        For j = 1 To tbl.Columns.Count
            tbl.Columns(j).Borders(wdBorderLeft).LineStyle = wdLineStyleNone
            tbl.Columns(j).Borders(wdBorderRight).LineStyle = wdLineStyleNone
        Next j
        
    Next tbl
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值