问题
I apologize for not having a program to show for my question, but I am in a serious pickle.
I am looking for a vba word program that searches for specific words and deletes the paragraph those words are in.
The aim of this exercise is to reduce the work load as I am using up to 1million plus words of text.
EDIT
I have created the program, could you please help me add a loop, to loop through the whole document?
Sub SelectRangeBetween()
Selection.HomeKey Unit:=wdStory
'Selection.TypeText Text:="hello"
' The Real script
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute findtext:="From: Research.TA@traditionanalytics.com", Forward:=True, Wrap:=wdFindStop 'this will initiate the start word
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
myrange.Start = myrange.Start
myrange.End = myrange.End + InStr(myrange, "This message has been scanned ") ' this will initiate the end word
myrange.Select
'Selection.Delete
End With
End Sub
回答1:
Try the following, it will search for TextToSearchFor and delete the paragraphs where it is found in the same document where you place the code (because I use the ThisDocument
object). Since you do not specify many details in your question, you will have to modify the TryMe
sub to fit your needs. Make sure you try it on a test document first! :)
Option Explicit
Sub TryMe()
Call RemoveParagraphs("TextToSearchFor")
End Sub
Sub RemoveParagraphs(psSearchString As String)
Dim oRng As Range
Set oRng = ThisDocument.Content
oRng.Find.Execute FindText:=psSearchString
While oRng.Find.Found
oRng.Select
Selection.Expand Unit:=wdParagraph
Selection.Delete
Set oRng = ThisDocument.Content
oRng.Find.Execute FindText:=psSearchString
Wend
End Sub
来源:https://stackoverflow.com/questions/34017538/remove-paragraph-containg-specific-wordsword-vba