2015-11-05 57 views
0

我正试图删除一组在发现所选文本后立即发生的句子(可能是1个或更多)。我使用Selection.Find来查找文本,但是我无法找到允许我在选择之后删除的任何内容,而不是删除选择本身。在VBA中发现在Word2010中选择后删除的文本

下面是代码片段:

 Selection.Find.ClearFormatting 
     With Selection.Find 
      .Text = req 
      .Replacement.Text = "" 
      .Forward = True 
      .Format = False 
      .MatchCase = False 
      .MatchWholeWord = True 
      .MatchAllWordForms = False 
      .MatchSoundsLike = False 
      .MatchWildcards = False 
     End With 

     'execute search 
     Selection.Find.Execute 

     If Selection.Find.Found Then 
      'Found the tag, check for a duplicate 
      Selection.Find.Execute 
      If Selection.Find.Found Then 
       'Found a duplicate 
       err = True 
       err_req = err_req & " " & req 
      Else 
       num_updated = num_updated + 1 
       'Update the requirements text 
       ' TODO: Delete the current requirements text 
       Selection.InsertAfter req_text_list(index) 
       'Set the requirement style 
       Selection.GoTo what:=wdGoToBookmark, Name:="\Para" 
       Selection.Style = ActiveDocument.Styles(STYLE_NAME_RQMT) 
       Selection.Collapse Direction:=wdCollapseEnd 
      End If 
     End If 

任何帮助将非常感激。谢谢:)

+0

看看这篇文章:http://stackoverflow.com/questions/497624/find-a-string-in-a-document-and-delete-everything-after - 它找到一个字符串,然后删除它后面的所有内容。你没有提到你如何决定删除多少句子。 –

回答

0

而不是删除单词,用“”代替单词会更好更快。我在下面的代码中使用了它。使用它按您的要求

Sub Wordsearchh() 
Dim rWord As Range 
Dim Search_Text As String 
Dim Next_Word_Rang As Range 
Set Next_Word_Rang = ActiveDocument.Range 


For Each rWord In ActiveDocument.Words 
    Search_Text = rWord.Text 

    Next_Word_Rang.Start = rWord.End 
    Next_Word_Rang.End = ActiveDocument.Range.End 
     With Next_Word_Rang.Find 

      .Execute FindText:=Search_Text, Forward:=True 

      If .Found = True Then 
      .Replacement.Text = "" 
      .Execute Replace:=wdReplaceAll 
      End If 
     End With 
Next 
End Sub