Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2005

Tip: Looking for answers? Try searching our database.

Deleting top 2 lines of each page

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
USAFA - 21 Feb 2005 23:27 GMT
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
Helmut Weber - 22 Feb 2005 04:00 GMT
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/
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.