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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help to delete specific lines from a text file

Status
Not open for further replies.

bosk00

Technical User
Mar 5, 2004
91
0
0
US
I have a text file from a legacy system that I am trying to clean up. I have opened the file in word and created a macro to make changes to some of the text, this works without any problems . I then created a macro to find the specific text I was searching for, and a second to delete 2 lines. I then combined them into a single macro. Here is the code.
Code:
 Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Total number of claims under audit"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
This works correctly for the first 2 pages, however on the third it no longer is deleteing the lines.

________________Example of p[age 3 before running macro_________________

Total number of claims under audit 3

Number of claims first filed during interim period 3

Non-expired prior claims under audit 0

___________________________________

In the above example I would like to remove the Total number of claims line and the blank line below it.

____________example of third page after running macro_____________
3

Number of claims first filed during interim period 3

Non-expired prior claims under audit 0
_________________

From this point forward to the end of the file it only removes the text I searched for and not the lines.

All help is appreciated, thank you in advance.

Alan
Senility at its finest
 
The following fixed the problem
Code:
Selection.Find.ClearFormatting
    With Selection.Find
        Do While (.Execute(findtext:="Total number of claims under audit", Forward:=True) = True) = True
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
        Loop
    End With

Alan
Senility at its finest
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top