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?
--
Terry Sycamore

RE: delete line containing a certain text string by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Thu Jun 26 13:57:00 PDT 2008

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


"Terry Sycamore" wrote:

> 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?
> --
> Terry Sycamore

Re: delete line containing a certain text string by Doug

Doug
Thu Jun 26 14:00:07 PDT 2008

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

"Terry Sycamore" <TerrySycamore@discussions.microsoft.com> wrote in message
news:4C23D520-AFC0-4481-8A61-D455BCB13355@microsoft.com...
>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?
> --
> Terry Sycamore



Re: delete line containing a certain text string by TerrySycamore

TerrySycamore
Fri Jun 27 10:18:01 PDT 2008

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.
--
Terry Sycamore


"Doug Robbins - Word MVP" wrote:

> 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
>
> "Terry Sycamore" <TerrySycamore@discussions.microsoft.com> wrote in message
> news:4C23D520-AFC0-4481-8A61-D455BCB13355@microsoft.com...
> >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?
> > --
> > Terry Sycamore
>
>
>

RE: delete line containing a certain text string by TerrySycamore

TerrySycamore
Fri Jun 27 10:34:00 PDT 2008

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?
--
Terry Sycamore


"StevenM" wrote:

> 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
>
>
> "Terry Sycamore" wrote:
>
> > 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?
> > --
> > Terry Sycamore

Re: delete line containing a certain text string by fumei

fumei
Fri Jun 27 12:48:49 PDT 2008

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 Sycamore wrote:
>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]
>> > 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?

--
Message posted via http://www.officekb.com


Re: delete line containing a certain text string by fumei

fumei
Fri Jun 27 12:58:50 PDT 2008

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

fumei wrote:
>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]
>>> > 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?

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200806/1


Re: delete line containing a certain text string by Doug

Doug
Fri Jun 27 14:12:46 PDT 2008

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

"Terry Sycamore" <TerrySycamore@discussions.microsoft.com> wrote in message
news:E688A81F-07A3-4AFD-9ABA-7FD2B5438F34@microsoft.com...
> 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.
> --
> Terry Sycamore
>
>
> "Doug Robbins - Word MVP" wrote:
>
>> 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
>>
>> "Terry Sycamore" <TerrySycamore@discussions.microsoft.com> wrote in
>> message
>> news:4C23D520-AFC0-4481-8A61-D455BCB13355@microsoft.com...
>> >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?
>> > --
>> > Terry Sycamore
>>
>>
>>



Re: delete line containing a certain text string by TerrySycamore

TerrySycamore
Fri Jun 27 16:12:01 PDT 2008

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!
--
Terry Sycamore


"Doug Robbins - Word MVP" wrote:

> 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
>
> "Terry Sycamore" <TerrySycamore@discussions.microsoft.com> wrote in message
> news:E688A81F-07A3-4AFD-9ABA-7FD2B5438F34@microsoft.com...
> > 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.
> > --
> > Terry Sycamore
> >
> >
> > "Doug Robbins - Word MVP" wrote:
> >
> >> 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
> >>
> >> "Terry Sycamore" <TerrySycamore@discussions.microsoft.com> wrote in
> >> message
> >> news:4C23D520-AFC0-4481-8A61-D455BCB13355@microsoft.com...
> >> >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?
> >> > --
> >> > Terry Sycamore
> >>
> >>
> >>
>
>
>