Results 1 to 6 of 6

Thread: Macro Help (2K)

  1. #1
    Star Lounger
    Join Date
    Dec 2003
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro Help (2K)

    I am stuck, and boy do I feel stupid about this...

    I want to write a macro that takes a selection, starts at the top of the selection, and loops until it gets to the end of the paragraph.
    Sort of:

    While NOT End_of_selection
    <do stuff>
    Wend

    How do I do this?

    Thanks,
    Saturn

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

    Re: Macro Help (2K)

    How do you want to loop through the text? By character, by word, by line?

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

    Re: Macro Help (2K)

    If you give us a better idea of what "do stuff" is then we may be able to come up with something better than a loop for you. If you need to loop then something like.
    <code>
    Dim wrdRange as Range

    For Each wrdRange in Selection.Words
    <font color=448800>' do stuff to wrdRange here</font color=448800>
    Next wrdRange
    </code>

    might be what you're looking for. You could also write something very similar for Sentences instead of Words, but avoiding the loop altogether is usually much better.

    StuartR

  4. #4
    Star Lounger
    Join Date
    Dec 2003
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro Help (2K)

    In an earlier part of the macro, several strings of text are replaced with XXX.

    The 1st XXX is replaced with the word "Further". The 2nd XXX is replaced with the string "Still further". The third XXX is replaced with "Additionally". The 4th XXX is replaced with "Moreover".

    Then the macro loops, going back to Further, still further, and so forth, until the end of the selection.

    Thanks,
    Shabtai

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Macro Help (2K)

    <pre> Dim rngX As Word.Range
    Dim lngCount As Long
    Dim strNewText As String
    Set rngX = Selection.Range
    With rngX
    .Collapse wdCollapseStart
    .Find.Text = "XXX"
    Do While .Find.Execute = True
    lngCount = lngCount + 1
    If .End > Selection.End Then
    Exit Do
    End If
    Select Case lngCount
    Case 1
    strNewText = "Further"
    Case 2
    strNewText = "Still further"
    Case 3
    strNewText = "Additionally"
    Case 4
    strNewText = "Moreover"
    lngCount = 0
    End Select
    .Delete
    .InsertAfter strNewText
    .Collapse wdCollapseEnd
    Loop
    End With

    Set rngX = Nothing</pre>


  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro Help (2K)

    Not sure what you're doing before/after, so you may need to adjust this.

    <pre>Sub XXX_Replace()
    Dim rng As Range
    Dim rngOrig As Range
    Set rngOrig = Selection.Range.Duplicate
    Set rng = Selection.Range.Duplicate
    With rng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Forward = False
    .Format = False
    .Text = "XXX(*)XXX(*)XXX(*)XXX"
    .Replacement.Text = "Further1Still Further2Additionally3Moreover"
    .Execute Replace:=wdReplaceAll
    End With
    rngOrig.Select
    End Sub
    </pre>


Posting Permissions

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