Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Word 2003 macro: Loop & EOF Error 52

    I need to repeat a set of recorded macro commands until the end of an active document is reached, but I encounter "runtime error 52"

    The coding is:

    Do While Not EOF(1)
    [recorded macro]
    Loop

    But I can't readily trace any Word macro guide which explains how the active document should be defined in this case. Any suggestions would be much appreciated.

    Dave.
    Last edited by purdyd; 2012-02-22 at 07:35. Reason: Syntax correction

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    There's more than one way to loop through the entire document content, but the best method would depend on what you're needing to accomplish with the "[recorded macro]" part - so: what's the overall purpose of the procedure, and what is the 'recorded macro' part intended to do?

    Gary

  3. #3
    Star Lounger
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Gary,

    Strip out some text irregularities for later conversion to a .CSV file.

    Dave.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Dave,

    Still helpful to know what the recorded macro part is doing - are you doing a Find/Replace?

    Gary

  5. #5
    Star Lounger
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Gary - a find, go back to beginning of line, step down, insert CR.

    Dave.

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Here's some code to do that:
    Code:
    Public Sub FindTextAndAddCR()
       Selection.HomeKey Unit:=wdStory
       With Selection.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[FindText]"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          If .Execute = True Then
             Selection.HomeKey Unit:=wdLine
             Selection.MoveDown Unit:=wdLine
             Selection.TypeParagraph
             Do Until Selection.Paragraphs(1).Range.End = ActiveDocument.Content.End
                If .Execute = True Then
                   Do Until Selection.Paragraphs(1).Range.End = ActiveDocument.Content.End
                      Selection.HomeKey Unit:=wdLine
                      Selection.MoveDown Unit:=wdLine
                      Selection.TypeParagraph
                   Loop
                End If
             Loop
          End If
       End With
    End Sub
    Note: This code could no doubt be made more efficient, but am at work at the moment so this is a quick effort!

    Gary
    Last edited by Gary Frieder; 2012-02-22 at 13:04. Reason: Deal with different contingencies

  7. The Following User Says Thank You to Gary Frieder For This Useful Post:

    purdyd (2012-02-22)

  8. #7
    Star Lounger
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Gary - many thanks, much appreciated.

    Dave.

  9. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Depending on your data structure, you may not even need a loop of any kind. If you could post some representative before/after samples, something quite efficient could be developed.

    FWIW, you can't use EOF within a Word document, as the metadata that manages all the formatting etc starts after the final paragraph break that you can see and can continue for many more lines.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #9
    Star Lounger
    Join Date
    Feb 2010
    Posts
    96
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    ... If you could post some representative before/after samples, something quite efficient could be developed. FWIW, you can't use EOF within a Word document ...
    Paul - many thanks for the offer, I'll ponder this possibility. Also, the EOF point is useful to know about.

    Dave.

Posting Permissions

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