• Do loop (Word XP)

    Author
    Topic
    #405156

    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.

    Viewing 1 reply thread
    Author
    Replies
    • #829596

      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

      • #830102

        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.

        • #830183

          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

          • #830290

            Hans,

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

          • #830291

            Hans,

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

          • #831980

            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.

            • #831992

              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.

            • #831998

              Hans,

              Fiddle I will! Thanks.

            • #832049

              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?

            • #832083

              (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.

            • #832113

              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.

            • #832125

              You attached an RTF file without macros confused

            • #832131

              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?

            • #832133

              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

            • #832147

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

            • #832148

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

            • #832153

              It is a .doc now, but still no macros. I just tested in the Test forum to make sure, there is no problem with posting a document with macros.

            • #832161

              I’m checking with our guys to see what could be wrong, but when I click Tools, Options, Macros, this is what I see….

            • #832165

              You can wrap the Word doc in a .zip file and attach that.

            • #832173

              It’s worth a shot. Hope it works.

              Thanks.

            • #832178

              jscher2000 has the answer!! I did store them in normal.dot.

              I will copy them to this document and then send. Now I get it.

            • #832196

              The way Word deletes text immediately after a hard page break is a bit strange.

              Frankly, I don’t understand why you want to delete 18 lines on the 2nd and following pages. By trial and error, the following seemed reasonable, but of course I don’t know your purpose.

              Sub HandlePage(intPage As Integer)
              Dim i As Integer
              Selection.MoveDown Unit:=wdLine, Count:=13 – (intPage > 1), Extend:=wdExtend
              Selection.Delete
              If intPage > 1 Then
              Selection.Delete
              End If
              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

              Note: Perhaps you also want to delete the last line on each page?

            • #832365

              Hans,

              I will gladly share the plan. Eventually this data is going to become records in Excel. I couldn’t get the formatting to work in Excel, so I am using Word as a formatting stage to strip out all unnecessary blank lines, so that I end up with a steady stream of the 5 fields.

              Once I get it into Excel there are other challenges because I now have to transpose the spot info with the numbers into 5 columns, plus take the Program title, time slot and Program number and combine all of that to create a complete record.

              That was my thinking. I’m all ears, so please share your thoughts with me.

            • #832379

              The Word document you posted yesterday looks like a report generated by another application. Does that application have an option to export to other formats, such as fixed width or delimited text file, Excel, DBase, …?

              If not, should the entire document become one table in Excel? If so, the following modification will convert the document to a form that can be copied and pasted into Excel, then converted to a table using Data | Text to Columns with the vertical bar | as delimiter.

              Sub HandlePage(intPage As Integer)
              Dim i As Integer
              Selection.MoveDown Unit:=wdLine, Count:=13 – (intPage > 1), Extend:=wdExtend
              Selection.Delete
              If intPage > 1 Then
              Selection.Delete
              End If
              For i = 1 To 6
              Selection.MoveDown Unit:=wdLine, Count:=5
              Selection.HomeKey
              Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
              Selection.Delete
              Next i
              Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
              Selection.Delete
              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
              Selection.HomeKey Unit:=wdStory
              With Selection.Find
              .ClearFormatting
              .Text = “^m”
              .Replacement.Text = “”
              .Replacement.ClearFormatting
              .MatchWildcards = False
              .Execute Replace:=wdReplaceAll
              End With
              End Sub

            • #832395

              Hans,

              The only option is .rtf and yes, it does come from a proprietary database application and creates this report. Thank you for your time. See attached to see the end result.

            • #832396

              Hans,

              The only option is .rtf and yes, it does come from a proprietary database application and creates this report. Thank you for your time. See attached to see the end result.

            • #1070591

              Hi, Hans. I have found your post during a search to find something that I can use to delete every third line of text from a document. I massaged your code and it worked great. Here comes the “except for” part of this. The document is 34 pages long and was created from a text file. After 6 pages, the macro stops. It stops at a point that doesn’t appear to be anything special. Any ideas what I may have done wrong? The only thing that I changed about your code was “If lngLine Mod 3 = 0 “. I have been tearing my hair out!! I appreciate your help. Thanks.

            • #1070592

              I tried it on a document with over a hundered pages and the macro ran to completion without problems, so without seeing the document it’s hard to say what goes wrong. By the way, if you want to delete every 3rd line, you should use

              If lngLine Mod 2 = 0 Then

              It might be easier to work with the text file directly. Do you want to save it as a text file again, or should the end result be a Word document?

            • #1070593

              Ultimately the text/document will be converted to a PDF. I will try to run it on a text file instead of a Word doc. I am just trying to make it easier on someone who has to go through these documents and perform this action on a regular basis. Thanks for the correction, but the way. I’ll keep you posted.

            • #1070594

              Here is a macro that will read a text file (specified by strInput) and write its lines with the exception of every third line to a new text file (specified by strOutput). It should be *much* faster than a macro that processes a Word document.

              Sub DeleteEveryThirdLine()
              ‘ Change the constants as needed
              Const strSource = “C:TestSource.txt”
              Const strTarget = “C:TestTarget.txt”
              Dim f1 As Integer
              Dim f2 As Integer
              Dim strLine As String
              Dim n As Long
              f1 = FreeFile
              Open strSource For Input As #f1
              f2 = FreeFile
              Open strTarget For Output As #f2
              Do While Not EOF(f1)
              Line Input #f1, strLine
              n = n + 1
              If n Mod 3 > 0 Then
              Print #f2, strLine
              End If
              Loop
              Close #f2
              Close #f1
              End Sub

              Note: this macro has … Mod 3 because it works differently.

              If it works correctly for you, you can add bells and whistles, such as prompting the user for the input and output files.

            • #1070598

              Your code works perfectly. However, I went back to the user and they need 2 things. A blank line needs to replace the deleted line so that there is an obvious space between each set of 2. Also, if the third line slated for deletion happens to fall on a blank line, then they need to delete the 4th line instead of the 3rd line. Again, any assistance is appreciated. Thanks.

            • #1070600

              Try changing the loop within the macro to

              Do While Not EOF(f1)
              Line Input #f1, strLine
              n = n + 1
              If n Mod 3 = 0 Then
              If strLine = “” Then
              n = n – 1
              Else
              Print #f2, “”
              End If
              Else
              Print #f2, strLine
              End If
              Loop

              Test thoroughly to see if the code handles all blank lines the way the user wants.

            • #1070601

              Here is another version. It will always output two non-blank lines followed by a blank line.

              Do While Not EOF(f1)
              Line Input #f1, strLine
              If Not strLine = “” Then
              n = n + 1
              If n Mod 3 = 0 Then
              strLine = “”
              End If
              Print #f2, strLine
              End If
              Loop

            • #1070603

              This is so cool! I am going to set up a test text file that meet the possible parameters that the user is concerned with and will let you know how it goes. This is exciting as it will save the user hours of work!! Thanks.

            • #1070605

              Is it possible to do an “else” statement in the do loop for the instance where the third line is blank and so the next line of text would not be printed, thus essentially deleting the 4th line instead of the third line?

            • #1070607

              Try both variants I posted – one of them should do what you want, I think.

            • #1070609

              It looks like the first iteration of the code is handling the entire project handily. The text file is extremely hard to read in the first place, so I need to test some more just to make sure that there are no weird happenings. Thanks for all of your help!! I couldn’t have done this without your assistance!

            • #832380

              The Word document you posted yesterday looks like a report generated by another application. Does that application have an option to export to other formats, such as fixed width or delimited text file, Excel, DBase, …?

              If not, should the entire document become one table in Excel? If so, the following modification will convert the document to a form that can be copied and pasted into Excel, then converted to a table using Data | Text to Columns with the vertical bar | as delimiter.

              Sub HandlePage(intPage As Integer)
              Dim i As Integer
              Selection.MoveDown Unit:=wdLine, Count:=13 – (intPage > 1), Extend:=wdExtend
              Selection.Delete
              If intPage > 1 Then
              Selection.Delete
              End If
              For i = 1 To 6
              Selection.MoveDown Unit:=wdLine, Count:=5
              Selection.HomeKey
              Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
              Selection.Delete
              Next i
              Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
              Selection.Delete
              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
              Selection.HomeKey Unit:=wdStory
              With Selection.Find
              .ClearFormatting
              .Text = “^m”
              .Replacement.Text = “”
              .Replacement.ClearFormatting
              .MatchWildcards = False
              .Execute Replace:=wdReplaceAll
              End With
              End Sub

            • #832366

              Hans,

              I will gladly share the plan. Eventually this data is going to become records in Excel. I couldn’t get the formatting to work in Excel, so I am using Word as a formatting stage to strip out all unnecessary blank lines, so that I end up with a steady stream of the 5 fields.

              Once I get it into Excel there are other challenges because I now have to transpose the spot info with the numbers into 5 columns, plus take the Program title, time slot and Program number and combine all of that to create a complete record.

              That was my thinking. I’m all ears, so please share your thoughts with me.

            • #832197

              The way Word deletes text immediately after a hard page break is a bit strange.

              Frankly, I don’t understand why you want to delete 18 lines on the 2nd and following pages. By trial and error, the following seemed reasonable, but of course I don’t know your purpose.

              Sub HandlePage(intPage As Integer)
              Dim i As Integer
              Selection.MoveDown Unit:=wdLine, Count:=13 – (intPage > 1), Extend:=wdExtend
              Selection.Delete
              If intPage > 1 Then
              Selection.Delete
              End If
              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

              Note: Perhaps you also want to delete the last line on each page?

            • #832179

              jscher2000 has the answer!! I did store them in normal.dot.

              I will copy them to this document and then send. Now I get it.

            • #832166

              You can wrap the Word doc in a .zip file and attach that.

            • #832169

              If you play with the drop-down in that dialog, you might find that the macros are stored either in the document’s attached template or in Normal.dot, rather than in the document itself. If the contents of the document are not essential to understand the code, you could open the editor and export the modules to .bas files. (For easiest posting, zip the .bas files together and upload the ZIP file.)

            • #832170

              If you play with the drop-down in that dialog, you might find that the macros are stored either in the document’s attached template or in Normal.dot, rather than in the document itself. If the contents of the document are not essential to understand the code, you could open the editor and export the modules to .bas files. (For easiest posting, zip the .bas files together and upload the ZIP file.)

            • #832162

              I’m checking with our guys to see what could be wrong, but when I click Tools, Options, Macros, this is what I see….

            • #832154

              It is a .doc now, but still no macros. I just tested in the Test forum to make sure, there is no problem with posting a document with macros.

            • #832134

              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

            • #832132

              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?

            • #832126

              You attached an RTF file without macros confused

            • #832114

              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.

            • #832084

              (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.

            • #832050

              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?

            • #831999

              Hans,

              Fiddle I will! Thanks.

            • #831993

              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.

          • #831981

            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.

        • #830184

          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

      • #830103

        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.

    • #829597

      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

    Viewing 1 reply thread
    Reply To: Do loop (Word XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: