Microsoft的在word中选择文档中的所有表格进行字体和格式的调整时的解决方案

找到宏

创建 并粘贴

使用

bas 复制代码
Sub 全选所有表格()
Dim t As Table
an = MsgBox("即将选择选区内所有表格,若无选区,则选择全文表格。", vbYesNo, "reboot提醒您!")
If an - 6 Then Exit Sub
Set rg = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
For Each t In rg.Tables
    t.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges wdEditorEveryone
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
End Sub

Sub 全选所有表格首行()
Dim t As Table
Dim firstRow As Row
an = MsgBox("即将选择选区内所有表格的首行,若无选区,则选择全文表格的首行。", vbYesNo, "reboot提醒您!")
If an - 6 Then Exit Sub
Set rg = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
For Each t In rg.Tables
    If t.Rows.Count >= 1 Then
        Set firstRow = t.Rows(1)
        firstRow.Range.Editors.Add wdEditorEveryone
    End If
Next
ActiveDocument.SelectAllEditableRanges wdEditorEveryone
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
End Sub

Sub 全选所有表格_调整格式()
    Dim t As Table
    Dim an As Integer
    Dim rg As Range
    Dim tableCount As Integer
    Dim cell As cell
    
    ' 确认提示
    an = MsgBox("即将格式化选区内所有表格,若无选区,则格式化全文表格。" & vbCrLf & _
                "操作包括:" & vbCrLf & _
                "1. 根据窗口调整宽度" & vbCrLf & _
                "2. 平均分布各行" & vbCrLf & _
                "3. 设置行高为1厘米" & vbCrLf & _
                "4. 设置单元格文本左对齐", vbYesNo, "reboot提醒您!")
    
    If an <> vbYes Then Exit Sub
    
    ' 确定操作范围
    Set rg = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
    tableCount = 0
    
    ' 直接遍历表格并应用格式
    Application.ScreenUpdating = False
    For Each t In rg.Tables
        ' 应用表格级格式
        t.AutoFitBehavior wdAutoFitWindow
        t.Rows.DistributeHeight
        t.Rows.Height = CentimetersToPoints(1)
        t.Rows.HeightRule = wdRowHeightExactly
        
        ' 遍历单元格设置对齐方式
        For Each cell In t.Range.Cells
            cell.VerticalAlignment = wdCellAlignVerticalCenter ' 垂直居中
            cell.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft ' 水平左对齐
        Next cell
        
        tableCount = tableCount + 1
    Next t
    Application.ScreenUpdating = True
    
    ' 提示结果
    If tableCount > 0 Then
        MsgBox "已成功格式化 " & tableCount & " 个表格!", vbInformation
    Else
        MsgBox "未找到可格式化的表格!", vbExclamation
    End If
End Sub
相关推荐
江沉晚呤时7 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
Oberon7 小时前
Avalonia硬配.NET Framework 4.8
c#·.net·avalonia·.net framework
喵叔哟10 小时前
3. 【Blazor全栈开发实战指南】--Blazor是什么?为什么选择Blazor?
c#·.netcore
钢铁男儿12 小时前
C# 接口(接口可以继承接口)
java·算法·c#
流形填表12 小时前
AI 助力:如何批量提取 Word 表格字段并导出至 Excel
开发语言·人工智能·word·excel·办公自动化
IT考试认证17 小时前
Microsoft AZ-305 Exam Question
microsoft·azure·az-305
小码编匠20 小时前
C# 的西门子数控系统 OPCUA 数据采集开发从零入门
后端·数据分析·c#
孜然卷k20 小时前
C#项目 在Vue/React前端项目中 使用使用wkeWebBrowser引用并且内部使用iframe网页外链 页面部分白屏
前端·vue.js·react.js·c#
专注VB编程开发20年21 小时前
C# VB.NET多进程-管道通信,命名管道(Named Pipes)
开发语言·c#·.net
唐青枫1 天前
C#.NET 泛型详解
c#·.net