Word宏操作(图片调整、删除表后空行)

脚本

全部图片样式调整

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
Sub 全部图片样式调整()
For Each pic In ActiveDocument.InlineShapes
pic.Select
With Selection.ParagraphFormat
.Alignment = wdAlignParagraphCenter '居中
.CharacterUnitFirstLineIndent = 0 '首行缩进转换为厘米
.FirstLineIndent = CentimetersToPoints(0) '设置首行缩进0厘米

.CharacterUnitLeftIndent = 0 '左侧缩进转换为厘米
.LeftIndent = CentimetersToPoints(0) '设置左侧缩进为0厘米

.CharacterUnitRightIndent = 0 '右侧缩进转换为厘米
.RightIndent = CentimetersToPoints(0) '设置右侧缩进为0厘米

.SpaceBeforeAuto = False '取消段前间距自动格式
.LineUnitBefore = 0 '段前间距转换为磅
.SpaceBefore = 0 '设置段前间距为0

.SpaceAfterAuto = False '取消段后间距自动格式
.LineUnitAfter = 0 '段后间距转换为磅
.SpaceAfter = 0 '设置段后间距为0

.LineSpacingRule = wdLineSpaceSingle '设置单倍行距
End With
Next pic
End Sub

InlineShapes用于选中所有插入的图片(包括visio对象),后续调整与表格调整原理相同

全篇删除表格后一行空行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Sub 全篇删除表格后一行空行()
Dim i As Integer
i = 0
For Each tempTable In ActiveDocument.Tables
With tempTable
'选中表格
.Select
'光标到表后一行
Selection.MoveDown Unit:=wdParagraph
'判断为空行则删除
If Selection.EndOf(Unit:=wdLine, Extend:=wdMove) = 0 Then
Selection.Delete
i = i + 1
End If
End With
Next tempTable
MsgBox ("共调整【" & i & "】处空行")
End Sub

Selection.EndOf(Unit:=wdLine, Extend:=wdMove)意为光标移动到最后所移动的字符个数