Deleting top 2 lines of each page

U

USAFA

I'm trying to put together a macro that goes to the beginning of the document
and deletes the top 2 lines (both extra blank lines needing to be removed),
then moves to the next page and deletes the top 2 lines, and so on until it
reaches the end of the document. Once it reaches the end, I want it to stop.
But I've got a problem, because it's not working. Please assist.

Here's my code so far:
Sub Step3()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.Find.ClearFormatting
ActiveDocument.Range(0, 0).Select
Do While .Execute
On Error Resume Next
Selection.Collapse Direction:=wdCollapseStart
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
Selection.Delete
Selection.MoveEndUntil Cset:=vbCr, Count:=wdForward
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If Selection.Range.End = ActiveDocument.Range.End Then
ActiveDocument.Range(0, 0).Select
Exit Sub
End If
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Collapse wdCollapseEnd
Loop
ActiveDocument.Range(0, 0).Select
End Sub
 
H

Helmut Weber

Hi,
how about this one:

Sub delete2lines()
Dim l As Long
Dim i As Long
With ActiveDocument
For l = .BuiltInDocumentProperties("number of pages") To 1 Step -1
For i = 1 To 2
With Selection
.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=l
.Bookmarks("\line").Select
.Delete
End With
Next
Next
End With
End Sub

Goto the page again, having deleted the first line,
may be necessary, as the selection jumps to the previous
page in some cases. And looping backwards may be essential,
as otherwise deleting lines may cause repagination, in so far,
as the previous two first lines on page x + 1 may become the
last two lines on a page after a deletion.
You may get an error, if there are only 2 lines on the
last page. But you surely can handle that, I think.
Using range instead of selection, causes an error (5904), too.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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