我有一个导出的Word文档,其中由数据库提取器构造的表在单元格中的包裹行之间有空格,我可以通过选择表并使用段落对话框来删除,但是有很多表我想自动执行此操作
选择文档中的所有表格(我可以用VBA做)之后我要做的就是设置Add Space Before和Add Space After both = 0,我认为,秘密地设置了AddSpaceBeforeAuto = AddSpaceAfterAuto = False
所以我开始使用一个简单的select子程序:
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成员来模仿我在Paragraph对话框中设置这些属性为零和false。
我尝试了三种方法: 1.为Normal样式(所有表使用的)全局设置值 2.选择每个表的值 3.在选择所有表格后,在总选择上设置值。
当我在selecttables()
执行后手动执行此操作时,我正在执行方法3。
下面的函数实际上尝试了所有三种方法。我有选择地对它们进行了评论,并发现没有一种方法有效,三种方法都没有帮助。
我尝试了两个"使用Selection.Range.Style.ParagraphFormat"和"使用Selection.Range.ParagraphFormat"对于方法3,但都没有奏效。
我还想设置表属性,"允许行在页面之间断开"为假(因为,严重的是,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
答案 0 :(得分:0)
我参考了我用过的表格参考,对方法3进行了拙劣的讨论 方法2而不是当前的选择。这是正确的答案:
Sub FixTables()
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)
With Selection.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
Application.ScreenUpdating = True
End Sub