【20240309】WORD宏设置批量修改全部表格格式

WORD宏设置批量修改全部表格格式

  • 引言
    • [1. 设置表格文字样式](#1. 设置表格文字样式)
    • [2. 设置表格边框样式](#2. 设置表格边框样式)
    • [3. 设置所有表格边框样式为075pt](#3. 设置所有表格边框样式为075pt)
    • [4. 删除行](#4. 删除行)

引言

这两周已经彻底变为office工程师了,更准确一点应该是Word工程师,一篇文档动不动就成百上千页,表格图片也是上千个之多,一个个手动该那估计改到归西那天我也改不完,所以顺手梳理了一下宏关于表格字体与边框格式设置的用法,以备后续不时之需。

后面也可能会不定时更新。

1. 设置表格文字样式

javascript 复制代码
// 设置所有文字样式和局部文字样式
Sub EditTablesFont()
'
' 设置所有的表格字体
'
'
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 = "Times New Roman" '西文字体
      .Size = 10 '字号
      .Bold = False '字形 不加粗
      .Italic = False '字形 不是斜体号
      .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
    With t.Rows(1)
        .Shading.BackgroundPatternColor = -654245991 '设置第一行的背景颜色为淡绿色
        With .Range.Font '设置第一行表头字体格式
            .NameFarEast = "黑体" '中文字体
            .NameAscii = "Times New Roman" '西文字体
            .Size = 10 '字号
            .Bold = False '字形 不加粗
        End With
    End With
  End With
Next i

End Sub;

2. 设置表格边框样式

javascript 复制代码
// 设置所有文字样式和局部文字样式
Sub EditTablesBorders()
'
' 设置所有的表格边框
'
'
    '遍历所有表格
    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

3. 设置所有表格边框样式为075pt

javascript 复制代码
// 设置所有文字样式和局部文字样式
Sub EditTablesBorders2()
'
' 设置所有的表格边框
'
'
    '遍历所有表格
    For Each tbl In ActiveDocument.Tables
    
        For i = 1 To tbl.Rows.Count
            tbl.Rows(i).Borders(wdBorderTop).LineStyle = wdLineStyleSingle
            tbl.Rows(i).Borders(wdBorderTop).LineWidth = wdLineWidth075pt
            tbl.Rows(i).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
            tbl.Rows(i).Borders(wdBorderBottom).LineWidth = wdLineWidth075pt
        Next i
        
    Next tbl
 
End Sub

4. 删除行

javascript 复制代码
// 设置所有文字样式和局部文字样式
Sub 删除行()

Dim myTab As Table

For Each myTab In ActiveDocument.Tables

    myTab.Rows(1).Delete '删除第一行

Next

End Sub

参考

https://blog.csdn.net/HyEidolon/article/details/134154144

相关推荐
辣香牛肉面8 小时前
[Windows] 搜索文本2.6.2(从word、wps、excel、pdf和txt文件中查找文本的工具)
word·excel·wps·搜索文本
FlYFlOWERANDLEAF1 天前
DevExpress中Word Processing Document API学习记录
学习·c#·word
AnySpaceOne1 天前
PDF转Word在线转换教程:多种实用方法分享
学习·pdf·word
我命由我123453 天前
Word - Word 的 5 种视图(页面视图、阅读视图、Web 版式视图、大纲视图、草稿视图)
ui·word·excel·photoshop·表格·ps·美工
XYZLHL3 天前
Word怎么设置页码总页数不包含封面和目录页
word
传而习乎3 天前
Word添加图/表题注
word
YAY_tyy3 天前
基于 Vue3 + VueOffice 的多格式文档预览组件实现(支持 PDF/Word/Excel/PPT)
前端·javascript·vue.js·pdf·word·excel
东风西巷4 天前
Atlantis Word Processor:全方位的文字处理专家
前端·学习·word·软件需求
Metaphor6924 天前
Java 高效处理 Word 文档:查找并替换文本的全面指南
java·经验分享·word