Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Searching within a range (WORD 97/2000)

    Hi,

    I have defined a macro that searches a given range (start to end of document) for a selected text with a particular style. If the text is found, it removes the line numbering etc. The macro is declared as below.

    The problem is that it only finds the FIRST instance of the selected style (i.e. even though I have defined the style multiple times in the document). It goes through the 'While' loop only ONCE. Can someone tell me if I have got something wrong with the macro or is a known problem. (BTW: I know I can do something similar using 'Selection' & it works perfectly well).

    Thanks for all your help and comments.

    ---------------------------------------------------------------------------------------------------------------------------------------------
    Sub Appendectomy ()

    Dim myRange As Range

    Set myRange = ActiveDocument.Range(Start:=0, End:=ActiveDocument.Content.End)

    myRange.Find.ClearFormatting
    myRange.Find.Style = ActiveDocument.Styles("Heading 1")
    myRange.Find.ParagraphFormat.Borders.Shadow = False
    With myRange.Find
    .Text = "Appendix"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With

    While myRange.Find.Execute()
    myRange.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
    myRange.ParagraphFormat.FirstLineIndent = InchesToPoints(-0.98)
    Wend

    End Sub
    --------------------------------------------------------------------------------------------------------------------------------------------
    Thanks.
    Robie

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

    Re: Searching within a range (WORD 97/2000)

    Hi,

    Just a quick guess as I haven't tested the code: try changing wrap to:

    .Wrap = wdFindStop

    Gary

  3. #3
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Searching within a range (WORD 97/2000)

    Thanks for quick response but it still doesn't work.
    Thanks.
    Robie

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Searching within a range (WORD 97/2000)

    This is strange. I made a test document, and if this line is in there:

    myRange.Find.ParagraphFormat.Borders.Shadow = False

    nothing is found. If I comment it out, the loop runs forever on the same paragraph. Hmmm...

    What happens if you change your loop like this?

    While myRange.Find.Execute()
    myRange.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph
    myRange.ParagraphFormat.FirstLineIndent = InchesToPoints(-0.98)
    myRange.Collapse wdCollapseEnd 'move beyond the previously found range
    Wend

    Mine hits every one of the matches, but then hits them again and again. But, when I then change the .Wrap parameter as described in Gary's post, it works as expected.

  5. #5
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Searching within a range (WORD 97/2000)

    Your suggestion works so thanks to you and Gary.

    Much obliged, gentlemen. Thank you very much.
    Thanks.
    Robie

Posting Permissions

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