2017-04-24 20 views
2

我有一個包含多個表的文檔。某些表格的書籤名稱以「NTS」開頭,其他表格未加書籤。我創建了一個循環的負面步驟,刪除名稱以「NTS」開頭的所有表;但是,如果表沒有書籤,則會彈出錯誤(不是意外)。所以我想測試一個表是否有關聯的書籤,如果沒有,轉到下一個表。我的if語句試圖做到這一點不起作用(星號吼聲)。有什麼建議麼?如果文檔中的每個表存在書籤,則測試

Dim tableCount As Integer 
Dim tableNumber As Integer 
Dim bookmarkName As String 
' 
tableCount = ActiveDocument.Tables.Count 
' 
For tableNumber = tableCount To 1 Step -1 
    ActiveDocument.Tables(tableNumber).Select 
    *If Selection.Bookmarks(1).Exists = False Then 
     GoTo Furthermore 
    End If* 
    bookmarkName = Selection.Bookmarks(1).Name 
    If Left(bookmarkName, 3) = "NTS" Then 
     ActiveDocument.Tables(tableNumber).delete 
     ActiveDocument.Bookmarks(bookmarkName).delete 
    End If 
Furthermore: 
Next 
+0

你在Word或VB.net中使用這個嗎?看起來像word-vba給我。 – Masoud

+0

是的,我正在使用Word VBA。而且,它看起來就是這樣做的。多謝你的幫助! – DAn

回答

0

用這個代替(檢查方法here):

If Selection.Bookmarks.Count = 0 Then GoTo Furthermore 

您要使用的一個需要一個自變量,書籤名稱,如下面的(檢查出來here):

Selection.Bookmarks.Exists("NTS_example") 
相關問題