2016-08-19 14 views
5

我有一個內容控件包含在一個重複節內容控件內的單詞模板。我需要創建一個按鈕來刪除項目中繼器,如添加。通過docx文件的宏中的索引以編程方式刪除重複節項?

我想弄清楚如何刪除重複節項。但在這種情況下 - 我總是刪除最後一個項目。但是,我希望能夠刪除用戶選擇的項目。

Sub delete() 
    Dim cc As ContentControl 
    Dim Index 
    Set cc = ThisDocument.SelectContentControlsByTag("ResolRepeater").Item(1) 
    With cc 
     .LockContentControl = False 
     .LockContents = False 
     .AllowInsertDeleteSection = True 

     For Index = 1 To cc.RepeatingSectionItems.Count 
     If Selection.Range.InRange(cc.RepeatingSectionItems(Index).Range) Or cc.RepeatingSectionItems(Index).Range.InRange(Selection.Range) Then 
      Exit For 
     End If 
     Next Index 

     'can't delete, get Run-Time Error '5904': "you can not change the range" 
     cc.RepeatingSectionItems(Index).Range.delete 

     'this lines always delete last element: 
     'cc.RepeatingSectionItems(Index).Range.Select 
     'Selection.Delete 

    End With 
End Sub 

word template

我會很樂意回答任何..

回答

1

你可以這樣做多種方式,其中一種是處理的事件ContentControlBeforeDeleteMSDN Link

或者,你可以使用當前的Selection.range,檢查範圍(包括開始和結束)是否有wdContentControlRepeatingSection類型的內容控制,然後簡單地刪除該控件。

類似的信息(代碼未測試):

var contentControl = Selection.Range.ContentControls; 
if (contentControl.Type == Microsoft.Office.Interop.Word.WdContentControlType) 
{ 
    contentControl.Delete(); 
} 
相關問題