|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 选中所有表格()
Dim T As Table
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
For Each T In ActiveDocument.Tables
T.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges wdEditorEveryone
ActiveDocument.DeleteAllEditableRanges wdEditorEveryone
End Sub
*** 以下宏为循环遍历文档中所有表格,一个一个地处理,可选用:
Sub 表格处理()
On Error Resume Next
Dim i As Long
i = ActiveDocument.Tables.Count
If i = 0 Then MsgBox "当前文档无表格!", vbOKOnly + vbCritical, "表格处理": Exit Sub
Dim a As Long, b As Long, c As String, h As String, s As String, t As Table, n As Long
c = MsgBox("是:自动 否:自定义 取消:放弃", vbYesNoCancel + vbExclamation, "表格处理")
If c = vbYes Then
h = 0.9
s = 12
a = 1
b = 1
ElseIf c = vbNo Then
h = InputBox("请输入表格行高值:(0.7-1.2 厘米比较美观)", "表格处理", "0.9")
If h = "" Then Exit Sub
s = InputBox("请输入表格内文字字号:(比正文小半号比较美观)" & vbCr & "三号/16磅,小三/15磅,四号/14磅,小四/12磅,五号/10.5磅", "表格处理", "12")
If s = "" Then Exit Sub
If MsgBox("根据内容调整表格吗?", vbYesNo + vbExclamation, "自动调整") = vbYes Then a = 1
If MsgBox("所有表格表头加粗吗?", vbYesNo + vbExclamation, "表头加粗") = vbYes Then b = 1
Else
Exit Sub
End If
If Selection.Information(wdWithInTable) = True Then Selection.Tables(1).Select: n = 1
For Each t In ActiveDocument.Tables
If n = 1 Then Set t = Selection.Tables(1) Else t.Select
' 表格标准化
With t
With .Rows
.WrapAroundText = False
.Alignment = wdAlignRowLeft
.HeightRule = wdRowHeightAtLeast
.Height = CentimetersToPoints(h)
End With
.AutoFitBehavior (wdAutoFitWindow)
.AutoFitBehavior (wdAutoFitWindow)
With .Range
With .Cells
.DistributeWidth
.VerticalAlignment = wdCellAlignVerticalCenter
End With
.Font.Size = s
With .ParagraphFormat
.Alignment = wdAlignParagraphCenter
.CharacterUnitFirstLineIndent = 0
.FirstLineIndent = CentimetersToPoints(0)
.Space1
End With
End With
.Shading.BackgroundPatternColor = wdColorAutomatic
' 根据内容调整表格
If a = 1 Then
.AutoFitBehavior (wdAutoFitContent)
.AutoFitBehavior (wdAutoFitContent)
End If
.Select
.AutoFitBehavior (wdAutoFitWindow)
.AutoFitBehavior (wdAutoFitWindow)
' 表头加粗
If b = 1 Then
With .Rows(1).Range.Font
.Name = "黑体"
.Name = "Times New Roman"
.Bold = True
End With
End If
End With
Next
If n <> 1 Then Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub |
|