How to find and delete lines?

I

Ian

Using Word97.

In a document I need to find the first "Heading 1" line containing a
known string. Having found that line, I then need to delete it and all
the remaining lines in the document.

For example, if the search string is "123", and the document looks like
this (line numbers on the left):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 This is a para of normal style text containing the
2 string 123, but I want to ignore this para because
3 the 123 is not in a heading 1.
4 PROCEDURE 123 (in Heading 1 style)
5 More normal text under the heading.
6 And even more (end of document).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In this example, I want to delete lines 4, 5 and 6 (but not lines 2 or
3, even though they also contain the search string).

I have no prior knowledge of what is in the document. All I know for
certain is that somewhere there will a Heading 1 line containing the
search string.

Can someone point me in the right VBA direction please.
 
P

Peter Hewett

Hi Ian

This should work for you although I do not have Word 97 installed:

Public Sub DeleteFromText()
Dim rngFind As Word.Range

' Setup find to search the entire document
Set rngFind = ActiveDocument.Content
With rngFind.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Heading 1")
.Text = "123"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Do find...
If .Execute Then

' Delete from the start of the paragraph containing the
' search text to the end of the document
rngFind.Start = rngFind.Paragraphs(1).Range.Start
rngFind.End = ActiveDocument.Content.End
rngFind.Text = vbNullString
End If
End With

End Sub

When you say "line" I've actually assumed you really meant paragraph, if I'm incorrect in
this assumption please let me know. If I'm correct the above code will delete from the
start of the paragraph containing the text "123" using style "Heading 1" to the end of the
document.

HTH + Cheers - Peter
 
I

Ian

That helps a lot. Many thanks Peter.

--
Ian

Peter Hewett said:
Hi Ian

This should work for you although I do not have Word 97 installed:

Public Sub DeleteFromText()
Dim rngFind As Word.Range

' Setup find to search the entire document
Set rngFind = ActiveDocument.Content
With rngFind.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("Heading 1")
.Text = "123"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Do find...
If .Execute Then

' Delete from the start of the paragraph containing the
' search text to the end of the document
rngFind.Start = rngFind.Paragraphs(1).Range.Start
rngFind.End = ActiveDocument.Content.End
rngFind.Text = vbNullString
End If
End With

End Sub

When you say "line" I've actually assumed you really meant paragraph,
if I'm incorrect in
this assumption please let me know. If I'm correct the above code will
delete from the
start of the paragraph containing the text "123" using style "Heading
1" to the end of the
document.

HTH + Cheers - Peter
 

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