delete line containing a certain text string

T

Terry Sycamore

I receive files containing many lines starting with the word 'Table' and
ending with the string '== =='. These are interspersed with a few lines also
beginning with 'Table' but ending with various different strings. I want to
delete the lines endiing '== ==', leaving only the others.

Each line ends with a paragraph mark (after the strings mentioned above.)

I've tried to do this with the Help available in Word and the advice given
in this discussion group but so far failed. Can anybody help?
 
S

StevenM

To: Terry,

Sub DeleteParagraphs()
Dim sBegin As String
Dim sEnd As String

sBegin = "Table"
sEnd = "== ==" & vbCr

For Each aPara In ActiveDocument.Paragraphs
If Left(aPara.Range.Text, Len(sBegin)) = sBegin Then
If Right(aPara.Range.Text, Len(sEnd)) = sEnd Then
aPara.Range.Delete
End If
End If
Next aPara
End Sub
 
D

Doug Robbins - Word MVP

This should do it

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="== ==^p", Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop) = True
Selection.Paragraphs(1).Range.Delete
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Terry Sycamore

Thanks for the reply, Doug, but it doesn't work. Gives a 'compile error' and
the lines
Do While .Execute(Findtext:="== ==^p", Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop) = True
are highlighted red.
 
T

Terry Sycamore

Thanks for the reply, Steven, but it doesn't work as it stands. Stepping
through it, I find the line "If Right(aPara.Range.Text, Len(sEnd)) = sEnd" is
not evaluatiing as true. The previous condition with the word "Table" is and
I've made sure exactly the right string that precedes the paragraph mark is
in the variable, by cutting and pasting it. The string is actually "== ==
". Could it be because it ends with spaces?
 
F

fumei via OfficeKB.com

Here is variation of a solution.

Dim oPara As Paragraph
Dim j As Long
For Each oPara In ActiveDocument.Paragraphs
j = oPara.Range.Words.Count
If oPara.Range.Words(1) = "Table " _
And oPara.Range.Words(j - 1) = "==" Then
oPara.Range.Delete
End If
Next

It is VERY literal though. It has to be "Table " - i.e Table with a trailing
space, AND the final characters of the paragraph are "==".

Terry said:
Thanks for the reply, Doug, but it doesn't work. Gives a 'compile error' and
the lines
Do While .Execute(Findtext:="== ==^p", Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop) = True
are highlighted red.
This should do it
[quoted text clipped - 19 lines]
 
F

fumei via OfficeKB.com

And as another variation...

Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(Findtext:="Table", Forward:=True) _
= True
r.Expand Unit:=wdParagraph
If InStr(r.Text, "==" & Chr(13)) > 0 Then
r.Delete
End If
r.Collapse 0
Loop
End With
Here is variation of a solution.

Dim oPara As Paragraph
Dim j As Long
For Each oPara In ActiveDocument.Paragraphs
j = oPara.Range.Words.Count
If oPara.Range.Words(1) = "Table " _
And oPara.Range.Words(j - 1) = "==" Then
oPara.Range.Delete
End If
Next

It is VERY literal though. It has to be "Table " - i.e Table with a trailing
space, AND the final characters of the paragraph are "==".
Thanks for the reply, Doug, but it doesn't work. Gives a 'compile error' and
the lines
[quoted text clipped - 6 lines]
 
D

Doug Robbins - Word MVP

That is because the mail program has inserted a line break in a line of code
that should all be on one line in the VBE or have a VBE line break character
at the point where it breaks. Either place the cursor after the end of the
first line and press delete to get the command all on one line, or modify
the codes as follows by inserting a space then an underscore character after
True,

Do While .Execute(Findtext:="== ==^p", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop) = True


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Terry Sycamore

Whoaaaa!!! Now it works. Well, mostly... Sometimes it leaves one or two
lines, out of over 200 in a file - apparently at random. Under examination,
they seem exactly like all the others; a search finds the strings in them but
even executing the macro again doesn't get them. Wierd, huh? I don't expect
you to be able to answer it, though, and it's not a big deal - so Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top