2015-08-13 70 views
0

我想根據用戶輸入刪除單詞doc的整個部分/段落。基於用戶輸入刪除部分

該文件具有「1.1」和「1.1.1」和「1.2」等部分用戶將輸入「1.1.1」,宏將刪除該部分。是否可以對「1.1.1」進行宏搜索並刪除從1.1.1開始直到但不包括1.2的所有內容?

我開始這個,但需要創建一個開始和結束點來刪除。

Sub DeleteParagraphContainingString() 

Dim search As String 
search = "1.1.1" 

Dim para As Paragraph 
For Each para In ActiveDocument.Paragraphs 

    Dim txt As String 
    txt = para.Range.Text 

    If InStr(LCase(txt), search) Then 
     para.Range.Delete 
    End If 

Next 

End Sub 

回答

0

您可以使用Word的Find功能,然後選擇整個部分並將其刪除。我給你幾個代碼片段,你可以編織在一起,應用支票,等等:

ActiveDocument.Range(0, 0).Select    ' go to beginning of document 

Selection.Find.ClearFormatting 
With Selection.Find 
    .text = yourFindString 
    .Replacement.text = "" 
    .Forward = True 
    .Wrap = wdFindStop 
    .Format = False 
    .MatchCase = True 
    .MatchWholeWord = True 
    .MatchWildcards = False 
    .MatchSoundsLike = False 
    .MatchAllWordForms = False 
End With 

While (Selection.Find.Execute) 
    Selection.Bookmarks("\HeadingLevel").Select   ' select whole Section 
    title = Selection.Paragraphs(1).Range.text   ' get Section Title (text of first paragraph) 
    Selection.Delete 
Wend 

我希望這可以幫助。