Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Do loop (Word XP)

    Hello,

    I recorded a macro that formats a report by deleting every 6th line. It is working fine. I'd like to create a do loop that repeats the routine until it reaches EOF. Any help would be appreciated.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Do loop (Word XP)

    Try this. It may not be the most efficient or elegant code possible.

    Sub DeleteEverySixthLine()
    Dim lngLine As Long
    Dim lngEnd As Long
    Selection.HomeKey Unit:=wdStory
    Selection.EndKey Unit:=wdLine
    lngEnd = ActiveDocument.Bookmarks("EndOfSel").End
    Do
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.EndKey Unit:=wdLine
    If ActiveDocument.Bookmarks("EndOfSel").End = lngEnd Then
    Exit Do
    Else
    lngLine = lngLine + 1
    lngEnd = ActiveDocument.Bookmarks("EndOfSel").End
    End If
    If lngLine Mod 5 = 0 Then
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
    Selection.Delete
    End If
    Loop
    End Sub

  3. #3
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,
    I didn't know you did Word too! Good to hear from you. Thanks, your code works. I didn't give you the whole story because I was hoping I could expand upon the loop idea and do the rest of the work myself, but alas, it's a little too much. What's really going on is this: I need to delete the first 13 lines on every page, then delete every 6th line 6 times.

    I understand some of your logic: You are selecting the entire document so that you can know when you reach ("EndOf Sel") and I think that is how the lngEnd is defined and I think lngLine is your counter. What do I need to paste and modify to include the 13 deletions along with the 6th line deletion?

    Thanks.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Do loop (Word XP)

    The code I posted doesn't select the entire document, but it uses the built-in bookmark EndOfSel that represents the end of the current selection to see if it changes when you move down a line; it it doesn't, you have reached the document.

    Here is revised code to do what you want. It assumes that each page is long enough to be able to delete the first 13 lines and every 6th line 6 times. It does check when you have reached the last page of the document. The procedure HandlePage does one page, the procedure HandleDocument does the entire document (it calls HandlePage for each page). The latter is the one you must run.

    Test thoroughly on a copy of the real document!

    Sub HandlePage()
    Dim i As Integer
    Selection.MoveDown Unit:=wdLine, Count:=13, Extend:=wdExtend
    Selection.Delete
    For i = 1 To 6
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.HomeKey
    Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
    Selection.Delete
    Next i
    End Sub

    Sub HandleDocument()
    Dim lngStart As Long
    Selection.HomeKey Unit:=wdStory
    Do
    lngStart = ActiveDocument.Bookmarks("Page").Start
    HandlePage
    Selection.GoToNext What:=wdGoToPage
    If ActiveDocument.Bookmarks("Page").Start = lngStart Then
    Exit Do
    Else
    lngStart = ActiveDocument.Bookmarks("Page").Start
    End If
    Loop
    End Sub

  5. #5
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,

    Once again, you come through. Thank you very much for your time.

  6. #6
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,

    I'm getting the hang of this, and the HandlePage sub is working. I just have one glitch: I need this code:
    Selection.MoveDown Unit:=wdLine, Count:=13, Extend:=wdExtend
    Selection.Delete
    to only run when you first call up the routine (for the first page). The rest of the routine is fine, but for each subsequent page, I need to change the number from 13 (in the code above) to 18.

    So basically, how do you get a routine to run once and then ignore that part of the routine? Would I use a counter and set it to one? I could then have the 18 lines that separate each of the other pages to be part of the routine.

    Thanks.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Do loop (Word XP)

    I would indeed keep track op the page through a counter.

    Sub HandlePage(intPage As Integer)
    Dim i As Integer
    Dim intCount As Integer
    If intPage = 1 Then
    intCount = 13
    Else
    intCount = 18
    End If
    Selection.MoveDown Unit:=wdLine, Count:=intCount, Extend:=wdExtend
    Selection.Delete
    For i = 1 To 6
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.HomeKey
    Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
    Selection.Delete
    Next i
    End Sub

    Sub HandleDocument()
    Dim lngStart As Long
    Dim intPage As Integer
    Selection.HomeKey Unit:=wdStory
    Do
    intPage = intPage + 1
    lngStart = ActiveDocument.Bookmarks("Page").Start
    HandlePage intPage
    Selection.GoToNext What:=wdGoToPage
    If ActiveDocument.Bookmarks("Page").Start = lngStart Then
    Exit Do
    Else
    lngStart = ActiveDocument.Bookmarks("Page").Start
    End If
    Loop
    End Sub

    You may have to fiddle a bit to get it right.

  8. #8
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,

    Fiddle I will! Thanks.

  9. #9
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,

    Can you explain a couple of things? When I run the macro, the first page is perfect, but then everything is off. I'm noticing that the page breaks (which are manual and are included in the 18 lines) are still there, and lines that contain data are being deleted.

    What do these lines of code do?

    lngStart = ActiveDocument.Bookmarks("Page").Start
    HandlePage intPage
    Selection.GoToNext What:=wdGoToPage
    If ActiveDocument.Bookmarks("Page").Start = lngStart

    I wanted to avoid sending you the document and fix this myself, but I made need to send it in order for you to really see what I'm trying to accomplish. Would that be OK?

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Do loop (Word XP)

    (1) lngStart = ActiveDocument.Bookmarks("Page").Start
    (2) HandlePage intPage
    (3) Selection.GoToNext What:=wdGoToPage
    (4) If ActiveDocument.Bookmarks("Page").Start = lngStart Then

    Line (1) sets the lngStart variable to the position of the start of the current page.
    Line (2) processes the current page (i.e. deletes the first 13 or 18 lines, then 5 times deletes the 6th line of the remainder.
    Line (3) moves to the next page
    Line (4) compares the position of the start of the current page to the value stored in lngStart. If they are equal, we didn't really move down a page, so we must already be on the last page. Time to get out (Exit Do).

    If you can't solve this yourself, make a copy of the document, replace any sensitive information with dummy data, and attach it to a reply. It should be below 100 KB; if it's too large, make a zip file and attach that.

  11. #11
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,

    I'm disappointed in myself because I can read this and understand what is happening, yet I can't get it to work. I've attached the file that contains your macro, as well as my other macros (which will reveal my thought processes). The first macro in the list actually works, but it was created by recording keystrokes. Anyway, thanks for all of your help.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Do loop (Word XP)

    You attached an RTF file without macros <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  13. #13
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hans,

    To check myself when I got your email, I went into the above post, opened the attachment and clicked Tools, Macros, and there they are. I see them. Anyway, I saved the file I was working in to another folder and opened it up to make sure it contained the macros and it does. If you don't see them, I don't know what could be wrong. There's no way that macros get stripped out of a file, is there?

  14. #14
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Do loop (Word XP)

    How strange, I also see an RTF file with no Macros.

    Can you save your document as a Word Document and upload this .DOC file as an attachment.

    StuartR

  15. #15
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    New York, New York, USA
    Posts
    170
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Do loop (Word XP)

    Hello Stuart, Thanks for another set of eyes. Here is the file in .doc form

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •