Word Three-Line Table Macros / Word 三线表宏工具
Word Three-Line Table Macros / Word 三线表宏工具
A small set of Word VBA macros to quickly select all tables in a document and convert them into academic-style three-line tables (top border, header separator, bottom border only).
一组用于 Word 的 VBA 宏,可一键选中文档中的所有表格,并将其批量转换为学术论文常用的三线表样式(仅保留顶线、表头分隔线和底线)。
GitHub Repo: https://github.com/Karcen/word-macro-toolkit
中文
功能简介
-
SelectAllTables(选中所有表格):选中文档中包含全部表格的整个区域。 -
ConvertAllToThreeLineTable(批量转换三线表):一键将文档中所有表格转换为三线表样式。 -
ConvertCurrentToThreeLineTable(当前表格转换):仅将光标所在的表格转换为三线表。
三线表样式说明
|
|
|
|
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
运行环境
-
Microsoft Word 2010 及以上版本(Windows 和 Mac 均支持 VBA)。 -
需要在”信任中心”中启用宏。
安装方法
-
打开你的 Word 文档。 -
按 Alt + F11打开 VBA 编辑器。 -
在左侧”工程”窗口中右键单击你的文档(或 Normal)→ 插入 → 模块。 -
将宏代码粘贴到新建的模块中。 -
按 Ctrl + S保存。如果保存到文档本身,请另存为.docm(启用宏的文档格式)。
使用方法
-
在 Word 中按 Alt + F8打开宏对话框,选择宏并点击运行。 -
想要真正”一键”操作,可将宏添加到快速访问工具栏:文件 → 选项 → 快速访问工具栏 → 从下列位置选择命令:宏 → 添加。
注意事项
-
Word 不支持同时选中多个不连续的表格,所以 SelectAllTables实际选中的是从第一个表格起点到最后一个表格终点的整个区域,包含中间的正文。如果只是想批量格式化表格,直接运行ConvertAllToThreeLineTable即可。 -
线宽按学术论文常用规范设置为 1.5 磅 / 0.75 磅。如需调整,修改代码中的 wdLineWidth150pt和wdLineWidth075pt即可。 -
如果你的表头有两行(如带合并单元格的复合表头),将核心子程序中的 Rows(1)改为Rows(2),使分隔线落在第二行下方。 -
宏会清除单元格底纹。如需保留底纹,请删除 FormatAsThreeLineTable中的最后一行代码。
宏代码
'=====================================================' Macro 1: Select the region containing all tables'=====================================================Sub SelectAllTables() Dim doc As Document Set doc = ActiveDocument If doc.Tables.Count = 0 Then MsgBox "There are no tables in this document.", vbInformation, "Info" Exit Sub End If ' Word does not support selecting multiple non-contiguous tables,' so we select the entire range from the start of the first table ' to the end of the last table. Dim rngStart As Range, rngEnd As Range, rngAll As Range Set rngStart = doc.Tables(1).Range Set rngEnd = doc.Tables(doc.Tables.Count).Range Set rngAll = doc.Range(rngStart.Start, rngEnd.End) rngAll.Select MsgBox "Selected the region containing all tables. " & _"Total tables: " & doc.Tables.Count & ".", _ vbInformation, "Done"End Sub'=====================================================' Macro 2: Convert all tables to three-line tables'=====================================================Sub ConvertAllToThreeLineTable() Dim doc As Document Dim tbl As Table Dim n As Long Set doc = ActiveDocument If doc.Tables.Count = 0 Then MsgBox "There are no tables in this document.", vbInformation, "Info" Exit Sub End If Application.ScreenUpdating = False For Each tbl In doc.Tables FormatAsThreeLineTable tbl n = n + 1 Next tbl Application.ScreenUpdating = True MsgBox n & " table(s) have been formatted as three-line tables.", _ vbInformation, "Done"End Sub'=====================================================' Macro 3 (optional): Convert only the current table'=====================================================Sub ConvertCurrentToThreeLineTable() If Selection.Information(wdWithInTable) = False Then MsgBox "Please place the cursor inside a table first.", _ vbExclamation, "Info" Exit Sub End If FormatAsThreeLineTable Selection.Tables(1) MsgBox "The current table has been formatted as a three-line table.", _ vbInformation, "Done"End Sub'=====================================================' Core routine: Apply three-line table formatting' Top and bottom borders: 1.5 pt' Header separator: 0.75 pt'=====================================================Private Sub FormatAsThreeLineTable(tbl As Table) ' 1. Remove all existing borders With tbl.Borders .InsideLineStyle = wdLineStyleNone .OutsideLineStyle = wdLineStyleNone End With' 2. Top border of the first row (thick, 1.5 pt) With tbl.Rows(1).Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth150pt .Color = wdColorBlack End With ' 3. Bottom border of the first row (thin, 0.75 pt) - header separator With tbl.Rows(1).Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth075pt .Color = wdColorBlack End With' 4. Bottom border of the last row (thick, 1.5 pt) With tbl.Rows(tbl.Rows.Count).Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth150pt .Color = wdColorBlack End With ' 5. Clear any background shading from auto-applied table styles tbl.Shading.BackgroundPatternColor = wdColorAutomaticEnd Sub
许可证
MIT 许可证,可自由使用、修改和分发。
English
Features
-
SelectAllTables — Selects the region containing every table in the document. -
ConvertAllToThreeLineTable — Batch-converts every table in the document into a three-line table. -
ConvertCurrentToThreeLineTable — Converts only the table that currently contains the cursor.
Three-Line Table Style
|
|
|
|
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Requirements
-
Microsoft Word 2010 or later (Windows or Mac, both support VBA). -
Macros must be enabled in Trust Center settings.
Installation
-
Open your Word document. -
Press Alt + F11to open the VBA editor. -
In the Project pane on the left, right-click your document (or Normal) → Insert → Module. -
Paste the macro code into the new module. -
Press Ctrl + Sto save. If saving to the document itself, save as.docm(macro-enabled).
Usage
-
Press Alt + F8in Word to open the Macros dialog, choose a macro, and click Run. -
For one-click access, add the macro to the Quick Access Toolbar:File → Options → Quick Access Toolbar → Choose commands from: Macros → Add.
Notes & Limitations
-
Word does not support selecting multiple non-contiguous tables. SelectAllTablestherefore selects the full range from the first to the last table, including any text in between. If you only need to batch-format tables, runConvertAllToThreeLineTabledirectly. -
Line widths follow common academic conventions (1.5 pt / 0.75 pt). Adjust wdLineWidth150ptandwdLineWidth075ptin the code to change them. -
For tables with a two-row header (e.g., merged header cells), change Rows(1)toRows(2)in the core routine so the separator appears under the second row. -
The macro clears background shading. Remove the last line of FormatAsThreeLineTableif you want to keep existing shading.
Macro Codes
'=====================================================' Macro 1: Select the region containing all tables'=====================================================Sub SelectAllTables() Dim doc As Document Set doc = ActiveDocument If doc.Tables.Count = 0 Then MsgBox "There are no tables in this document.", vbInformation, "Info" Exit Sub End If ' Word does not support selecting multiple non-contiguous tables,' so we select the entire range from the start of the first table ' to the end of the last table. Dim rngStart As Range, rngEnd As Range, rngAll As Range Set rngStart = doc.Tables(1).Range Set rngEnd = doc.Tables(doc.Tables.Count).Range Set rngAll = doc.Range(rngStart.Start, rngEnd.End) rngAll.Select MsgBox "Selected the region containing all tables. " & _"Total tables: " & doc.Tables.Count & ".", _ vbInformation, "Done"End Sub'=====================================================' Macro 2: Convert all tables to three-line tables'=====================================================Sub ConvertAllToThreeLineTable() Dim doc As Document Dim tbl As Table Dim n As Long Set doc = ActiveDocument If doc.Tables.Count = 0 Then MsgBox "There are no tables in this document.", vbInformation, "Info" Exit Sub End If Application.ScreenUpdating = False For Each tbl In doc.Tables FormatAsThreeLineTable tbl n = n + 1 Next tbl Application.ScreenUpdating = True MsgBox n & " table(s) have been formatted as three-line tables.", _ vbInformation, "Done"End Sub'=====================================================' Macro 3 (optional): Convert only the current table'=====================================================Sub ConvertCurrentToThreeLineTable() If Selection.Information(wdWithInTable) = False Then MsgBox "Please place the cursor inside a table first.", _ vbExclamation, "Info" Exit Sub End If FormatAsThreeLineTable Selection.Tables(1) MsgBox "The current table has been formatted as a three-line table.", _ vbInformation, "Done"End Sub'=====================================================' Core routine: Apply three-line table formatting' Top and bottom borders: 1.5 pt' Header separator: 0.75 pt'=====================================================Private Sub FormatAsThreeLineTable(tbl As Table) ' 1. Remove all existing borders With tbl.Borders .InsideLineStyle = wdLineStyleNone .OutsideLineStyle = wdLineStyleNone End With' 2. Top border of the first row (thick, 1.5 pt) With tbl.Rows(1).Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth150pt .Color = wdColorBlack End With ' 3. Bottom border of the first row (thin, 0.75 pt) - header separator With tbl.Rows(1).Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth075pt .Color = wdColorBlack End With' 4. Bottom border of the last row (thick, 1.5 pt) With tbl.Rows(tbl.Rows.Count).Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth150pt .Color = wdColorBlack End With ' 5. Clear any background shading from auto-applied table styles tbl.Shading.BackgroundPatternColor = wdColorAutomaticEnd Sub
License
MIT License. Use, modify, and distribute freely.
夜雨聆风