【发布时间】:2015-06-13 14:36:58
【问题描述】:
我有一个导出的 Word 文档,其中由数据库提取器构建的表格在单元格中的换行之间有空格,我可以通过选择表格并使用段落对话框将其删除,但是有很多表格,我想自动执行此操作.
在选择文档中的所有表(我可以使用 VBA)后,我所要做的就是设置 Add Space Before 和 Add Space After both = 0,我认为这也偷偷地设置了AddSpaceBeforeAuto = AddSpaceAfterAuto = False。
所以我从一个简单的选择子程序开始:
Sub selecttables()
Dim mytable As Table
Application.ScreenUpdating = False
For Each mytable In ActiveDocument.Tables
mytable.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
Application.ScreenUpdating = True
End Sub
这很好用,我的所有表格都被选中。我现在要做的就是设置适当的 ParagraphFormat 成员,以模仿我在“段落对话框”中将这些属性设置为零和假。
我尝试了三种方法: 1. 为 Normal 样式全局设置值(所有表格都使用) 2.设置每个表的值,因为它们被选中 3. 选择所有表后,设置总选择的值。
当我在 selecttables() 执行后手动执行此操作时,我正在执行方法 3。
下面的函数实际上尝试了所有三种方法。我有选择地将它们注释掉,发现没有一种方法有效,并且所有三种方法都无济于事。
我为 METHOD 3 尝试了“With Selection.Range.Style.ParagraphFormat”和“With Selection.Range.ParagraphFormat”,但都没有成功。
我还想将表格属性“允许跨页换行”设置为 False(因为,说真的,True 的默认值真的很愚蠢!)而且也不知道该怎么做。
函数如下:
Sub FixTables()
Dim mytable As Table
Dim i As Integer
Application.ScreenUpdating = False
' METHOD 1:
ActiveDocument.Styles("Normal").ParagraphFormat.Space1
ActiveDocument.Styles("Normal").ParagraphFormat.SpaceAfter = 0
ActiveDocument.Styles("Normal").ParagraphFormat.SpaceBefore = 0
ActiveDocument.Styles("Normal").ParagraphFormat.SpaceAfterAuto = False
ActiveDocument.Styles("Normal").ParagraphFormat.SpaceBeforeAuto = False
For Each mytable In ActiveDocument.Tables
' METHOD 2:
With mytable.Style.ParagraphFormat
.Space1
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
End With
mytable.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
'
With Selection.Style.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
End With
Application.ScreenUpdating = True
End Sub
【问题讨论】:
标签: vba ms-word spacing paragraph word-2007