Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete certain lines of text in Word using macro or VB6 1

Status
Not open for further replies.

Morc

Technical User
Jul 6, 2003
7
GB
Does anyone know how to remove lines of text from word that match certain criteria? Either by using a Macro or in VB6?

The scenario is that an application at work produces items of work for my colleagues in the form of a list of information. They cut & paste this list into Word and then print the information, usually about four pages long. The thing is that a lot of the information is useless to them and the only parts they need would fit onto 1 sheet of paper. To get around this they have been manually removing the lines in Word before they print it out. They have targets to reach and this way is taking a lot of time. I would like to produce a Macro to do this for them. An example of the information is below:

WI_OUTPOST=RZ30 6AB
WI_OUTREFNO=1023921428368
WI_OUTFINALBILL=
WI_OUTFRWDNUMBER=<missing>

I would like the Macro or VB code to remove the lines that had no info i.e. WI_OUTFINALBILL= or WI_OUTFRWDNUMBER=<missing>

If anyone has any ideas I'd be grateful. I've tried using Find Replace but to no avail.

Thanks in Advance.

Marc
 
Hi Morc,

Word isn't good with &quot;lines&quot; but, Yes, it can be done. The lines in the post are separated with Chr(11) manual line breaks, but these can be changed to paragraph marks with a single Replace All, after which you can work with paragraphs, something like ..

Code:
Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs
If right(p.Range.Text,1) = &quot;=&quot; then ...
Next

I haven't time to do full code for it right now but should have later today; if you would like it please post back.

Enjoy,
Tony
 
I'd appreciate the full code.

Thanks
 
Hi Morc,

I think this should do it ..

Code:
Sub DeleteLines()
    
    Dim Para As Paragraph
    Dim p As Integer
    
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = &quot;^l&quot;
        .Replacement.Text = &quot;^p&quot;
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    For p = ActiveDocument.Paragraphs.Count To 1 Step -1
        Set Para = ActiveDocument.Paragraphs(p)
        If Left(Right(Para.Range.Text, 2), 1) = &quot;=&quot; _
        Or Left(Right(Para.Range.Text, 11), 10) = &quot;=<missing>&quot; Then
            Para.Range.Delete
        End If
    Next

End Sub

Enjoy,
Tony
 
Thanks

Works a treat.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top