Macro to remove lines containing specific text

A

Al

I would like to be able to remove all lines from a Word document which
contain a specific text string and copy them to a second file at the same
time.

Can someone suggest some code that could do that?

I have seen similar posts in this forum which are close to my needs but none
that show me the code needed to both remove the entire line and append it to
a new file.

Thanks in advance
Al
 
G

Graham Mayor

What is the string, and what do you mean by 'line'? A 'line' in Word is a
rather fluid concept the length of which is affected by a variety of
factors. Do you in fact mean 'paragraph'?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Al

By "string" I mean the same text will be found in each line
Each line can be considered a Paragraph
It is in fact a tabular list of hardware items but not in a Word table.
If that text is in the line then remove it from the file and build a second
file to store all the lines that are removed

You will end up with the original file minus those lines that contained the
text plus the second file which will contain all the removed lines

I hope that is clear and thank-you for taking the time to read my question.
Al
 
D

Doug Robbins - Word MVP

Try this

Dim Source As Document, Target As Document
Dim Findthis As String
Findthis = InputBox("Enter the text to be found", "Finder")
Set Source = ActiveDocument
Set Target = Documents.Add
Source.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:=Findthis, Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop) = True
Set myrange = Selection.Paragraphs(1).Range
Target.Range.InsertAfter myrange & vbCr
myrange.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
 

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