Word宏操作(空白格填充、序号检查)

脚本

单表空白格填充

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sub 单表空白格填充()
Dim CK1, CK2 As Boolean
Dim aCell As Cell
With Selection.Tables(1)
For Each aCell In .Range.Cells
aCell.Select
CK1 = (Selection.Range.ListFormat.ListString = "")
CK2 = (Selection.Text = (Chr(13) & Chr(7)))
If CK1 And CK2 Then
Selection.Text = "-"
End If
Next
End With
End Sub

全部表空白格填充

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sub 全部表空白格填充()
Dim CK1, CK2 As Boolean
Dim aCell As Cell
For Each tempTable In ActiveDocument.Tables
With tempTable
For Each aCell In .Range.Cells
aCell.Select
CK1 = (Selection.Range.ListFormat.ListString = "")
CK2 = (Selection.Text = (Chr(13) & Chr(7)))
If CK1 And CK2 Then
Selection.Text = "-"
End If
Next aCell
End With
Next tempTable
End Sub

使用ListFormat.ListStringSelection.Text分别对手动填写自动填充序号格式的内容进行判断

简单序号检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Sub 简单序号检查()
Dim CK1, CK2 As Boolean
For Each tempTable In ActiveDocument.Tables
With tempTable
'选中序号列
.Cell(1, 1).Select
If InStr(Selection.Text, "序号") <> 0 Then
'选中序号列第一个序号
.Cell(2, 1).Select
'手动编号1检测
CK1 = (Selection.Range.ListFormat.ListString = "") And Selection.Text = ("1" & Chr(13) & Chr(7))
'自动编号1检测
CK2 = (Selection.Range.ListFormat.ListString = "1") And Selection.Text = (Chr(13) & Chr(7))
If Not (CK1 Xor CK2) Then
MsgBox "发现问题序号"
Exit Sub
End If
End If
End With
Next tempTable
End Sub

序号检查页码提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Sub 序号检查页码提示()
Dim CK1, CK2 As Boolean
Dim Pages, PB, PN As String
Pages = ""
PB = ""
For Each tempTable In ActiveDocument.Tables
With tempTable
'选中序号列
.Cell(1, 1).Select
If InStr(Selection.Text, "序号") <> 0 Then
'选中序号列第一个序号
.Cell(2, 1).Select
'手动编号1检测
CK1 = Selection.Range.ListFormat.ListString = "" And Selection.Text = ("1" & Chr(13) & Chr(7))
'自动编号1检测
CK2 = (Selection.Range.ListFormat.ListString = "1") And Selection.Text = (Chr(13) & Chr(7))
If Not (CK1 Xor CK2) Then
PN = Selection.Information(wdActiveEndPageNumber)
If PB <> PN Then
PB = PN
Pages = Pages & PB & ","
End If
End If
End If
End With
Next tempTable
MsgBox ("问题页码:" & Pages)
End Sub

CK1、CK2分别对手动编号为1和自动编号为1进行判断,又因为同一格中同时使用手动和自动编号,会变成11,但两个判断均为True,因此使用异或运算(只有一个为True时为True)进行判定

序号显示采用字符串拼接方式(集合等方式对本功能来说太复杂)