2017-08-27 84 views
0

我剛開始使用VBA,使用Word 2010年我想用以下條件來修正表中的縮進:的Tbl.Rows.LeftIndent = 0.74文檔中 所有表進行更新,以便Tbl.Rows.LeftIndent = 0.在Word 2010中使用VBA來控制表格屬性

下面

的代碼生成以下錯誤:

"Object variable or With block variable not set."

代碼:

Sub FixEmbeddedTableIndent() 
Dim tbl As Table 
Dim blnFound As Boolean 
With ThisDocument.Range.Find 
tbl.Rows.LeftIndent = 0 
    Do 
     blnFound = .Execute 
     If blnFound Then 
      tbl.Rows.LeftIndent = 0.74 
Else 
      Exit Do 
     End If 
    Loop 
End With 
End Sub 

的有關如何編輯此代碼的建議?

注意:由於文檔中有兩種表格,因此需要此條件,並且宏應只更新一種表格。

回答

0

您不能使用Find方法來發現表格行縮進。而且,在任何情況下,在確定找到某件事之前,您都不能使用找到的對象。如果沒有發現,當然就沒有這樣的對象。

下面的代碼是一個更好的方法。

Sub FixEmbeddedTableIndent() 

    Dim Tbl As Table 

    ' Using ThisDocument will require you to paste the code into 
    ' every document in which you want to use it. 
    ' The ActiveDocument could be one which doesn't have this code. 

    With ActiveDocument 
     For Each Tbl In .Tables 
      With Tbl.Rows 
       If .LeftIndent = InchesToPoints(0.74) Then 
        .LeftIndent = 0 
       End If 
      End With 
     Next Tbl 
    End With 
End Sub 

請注意我使用的InchesToPoints()函數。 Word將縮進存儲爲點。當你看到縮進爲0.74(英寸?),這是一個近似的轉換。因此InchesToPoints(.74) - 可以達到53.28 - 可能不是實際設置的值。因此,我的代碼可能無法發現您之後的表格。您可能希望允許範圍在53到54點之間,甚至更大。

您可以更靈活地指定您希望通過以下任何方法重新調整的表格。

If .LeftIndent > 51 Then 
If .LeftInden < 60 Then 
If (.LeftIndent > 51) And (.LeftIndent < 60) Then 
+0

我們如何指定一系列點數?非常感謝 – SMark

+0

我向你的問題添加了一個答案給我的上述解決方案。 – Variatus

+0

工作!非常感謝! – SMark

相關問題