PDA

View Full Version : Word 2010 - Delete everything in the text after the second paragraph



leaoc
03-11-2016, 07:14 PM
Hey everyone!

I was hoping to get some help on a problem I'm trying to solve.

I have a document with several paragraphs and I want to run a macro to delete everything in the entire document after the second paragraph.

Any help is appreciated :)

leaoc
03-11-2016, 07:25 PM
Btw, this is what I have at the moment, but I have to run the macro several times to delete everything


Sub Test()

Dim i As Long, Count As Long


For i = 6 To ActiveDocument.Paragraphs.Count
Dim myRange As Range
Set myRange = ActiveDocument.Paragraphs(i).Range
myRange.SetRange myRange.Start, myRange.End
myRange.Delete
Next i


End Sub

gmayor
03-11-2016, 09:30 PM
The following will delete all in the document body except the first two paragraphs.

Sub Macro1()
Dim orng As Range
Set orng = ActiveDocument.Range
orng.MoveStart wdParagraph, 2
orng.Delete
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub
Your way would be slower, but if you want to go along that route, delete from the end.

Sub Macro2()
Dim i As Long
For i = ActiveDocument.Paragraphs.Count To 3 Step -1
ActiveDocument.Paragraphs(i).Range.Delete
Next i
lbl_Exit:
Exit Sub
End Sub