2015-04-08 63 views
0

我有一個導出的Word文檔,其中由數據庫提取器構造的表格在單元格中包裝的行之間有空格,我可以通過選擇表格並使用段落對話框,但有很多表,我想自動執行此操作。我想爲Word 2007文檔中的所有表格設置空格間隔

在選擇文檔中的所有表格後(我可以用VBA做到的),我必須要做的事情是設置添加空間前和添加空間在= 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成員,以將段落對話框中這些屬性的設置模擬爲零和false。

我嘗試了三種方法: 1.全局配置值Normal樣式(其中所有的表使用) 2.如它們被總選擇所選 3.設置值設置爲每個表中的值,在所有表被選中之後。執行

當我這樣做selecttables()後手動,我做的方法3.

下面實際上是嘗試所有這三種方法的功能。我有選擇性地評論他們,發現沒有任何一種方法可行,三者都不起作用。

對於方法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 

回答

0

我拙劣方法3中,通過參照在我METHOD 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 
相關問題