Macro for deleting multiple lines between words that won't always be the same

Anonymous
2020-10-25T04:49:29+00:00

I'm trying to find a macro or find and replace string that will help me select and delete a bunch of junk lines from some stuff I have to copy. I can't copy this from the source any other way, it forces the whole text on you like this.

Every bit of content is different so there aren't any constants on either side of the text I want to delete. The only constant in all of these are the words "Full Text" and the placement of the paragraph breaks. This is an example, I bolded the bits I'm trying to remove:

randomtext**¶**

randomtext¶

random numbers¶

Full Text¶

randomtext

These are consistently the first 10 lines of every bit of pasted content, but I only want to keep lines 1 and 10. Every pattern I've tried with find and replace fails to target this text and only this text. Is there any VBA code I could use to accomplish this? Thanks.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
HansV 462.7K Reputation points MVP Volunteer Moderator
2020-10-25T10:34:25+00:00

Try this. Please test on a copy of your document first!

Sub DeleteText()

    Application.ScreenUpdating = False

    Selection.HomeKey Unit:=wdStory

    With Selection.Find

        .ClearFormatting

        .Replacement.ClearFormatting

        .Text = "Full Text"

        Do While .Execute

            Selection.MoveStart Unit:=wdParagraph, Count:=-5

            Selection.MoveEnd Unit:=wdParagraph, Count:=3

            Selection.Delete

        Loop

    End With

    Application.ScreenUpdating = True

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2020-10-25T18:04:37+00:00

    That worked! Many thanks.

    Was this answer helpful?

    0 comments No comments