PDA

View Full Version : how to delete lines in word that do not have a certain character



mdnl
07-18-2013, 09:14 AM
Hello members,
I need some help with the following
I have a large word document and I need to delete lines/paras that do not contain a certain word.
Any ideas how to do this with VBA.
Thanks
A total newbie

fumei
07-19-2013, 04:10 PM
Sub RemoveParaWithWord()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If InStr(1, oPara.Range.Text, "blah") > 0 Then
oPara.Range.Delete
End If
Next
End Sub

Removes any paragraph that contains the word "blah".

adlena
07-22-2013, 03:56 AM
nice and useful post, thanks for sharing

fumei
07-22-2013, 08:30 PM
Oooops, I forgot you wanted something for paragraphs that do NOT have a text. My previous code deleted paragraphs that DID have text.
Sub RemoveParaWithOUTWord()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If InStr(1, oPara.Range.Text, "blah") = 0 Then
oPara.Range.Delete
End If
Next
End Sub All paragraphs that do NOT have "blah" are deleted.

Can you see the difference? The function InStr checks for the search string - "blah".

If the result is greater than 0 - InStr(1, oPara.Range.Text, "blah") > 0 - then the string DOES have the search string.

OR.....

If the result = 0 - If InStr(1, oPara.Range.Text, "blah") = 0 - then the string does NOT have the search string.

Megan36
09-03-2013, 10:12 PM
The two series, while on the surface seemed mere parody and humor, containing anticipation of what was to come for the characters involved. Blue Beetle made ​​a point of saying that he would die if he had joined the team,Mary Marvel very similar to the dark current of Mary Marvel.

fumei
09-04-2013, 08:26 PM
Troll. Go somewhere else. Or post something useful...not likely. Why do you even bother? Stupidity.