Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Jan 2003
    Location
    San Francisco, California, USA
    Posts
    93
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Find and Replace/Locating Spaces (2000/Service Pac

    <P ID="edit" class=small>(Edited by jscher2000 on 10-Nov-04 13:17. )</P>Hello All,

    Is there anyway of getting Word's Find and Replace or VBA to tell the difference between a space chrw(32) and a nonbreaking space chrw(160)? My code below keeps getting caught in a loop on the first find.

    This is my code:<pre>----------------------------------------------------------
    Sub Liter()
    Dim objDoc As Document
    Dim oRange As Range
    Set objDoc = ActiveDocument
    Set oRange = objDoc.Content

    DoFind oRange, "^#" & Chr(32) & "L"
    While oRange.Find.Found
    ReplaceSpace oRange
    Set oRange = objDoc.Content
    DoFind oRange, "^#" & Chr(32) & "L"
    Wend

    If Not (objDoc Is Nothing) Then Set objDoc = Nothing
    If Not (oRange Is Nothing) Then Set oRange = Nothing
    End Sub
    ---------------------------------
    Private Sub DoFind(oRange As Range, sText As String)
    With oRange.Find
    .ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .MatchCase = True
    .Execute FindText:=sText
    End With
    End Sub
    ---------------------------------
    Public Function ReplaceSpace(oRange As Range)
    With oRange.Find
    .Text = ChrW(32)
    .Replacement.Text = "^s"
    .Execute Replace:=wdReplaceAll
    End With
    oRange.Collapse Direction:=wdCollapseEnd
    End Function
    -------------------------------------</pre>

    Edited: <!t>[pre]<!/t> and <!t>[/pre]<!/t> tags added around the code block to preserve indenting. -Mod.

  2. #2
    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: Find and Replace/Locating Spaces (2000/Service Pac

    I don't know a simple answer to the main question raised by your post (i.e.: How do you search for Chr(32) and not find Chr(160)?), but I think you can duck the question for purposes of your Liter procedure. Just get rid of the "Set oRange = objDoc.Content" line and see if it does what you want.

  3. #3
    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: Find and Replace/Locating Spaces (2000/Service Pac

    As a supplement to my first reply, here's an alternative approach:
    <pre> Dim oRng As Word.Range

    Set oRng = ActiveDocument.Range(0, 0)

    With oRng.Find
    .Text = "^# L"
    .Forward = True
    .Wrap = wdFindStop
    Do While .Execute = True
    If Asc(oRng.Characters(2)) = 32 Then
    oRng.Characters(2) = Chr(160)
    oRng.Collapse wdCollapseEnd
    End If
    Loop
    End With

    Set oRng = Nothing</pre>


  4. #4
    Star Lounger
    Join Date
    Jan 2003
    Location
    San Francisco, California, USA
    Posts
    93
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Find and Replace/Locating Spaces (2000/Service Pac

    Yes! This does work, however, I am even more perplexed about using ranges with Find and Replace. Just when I think I understand how they work, I find that I do not. The reason my first example did not work is that when my ReplaceSpace function collapsed the range to the end, the next Find would be false because the range would be collapsed to a single character. This is why I reset the range to the entire document before doing another find. When I reset the range the next find would locate the previous find and I would be stuck in a loop. That is why I made this post to see If I could get Word to distinguish a chr(32) from a chr(160).

    What I don't understand about your brilliant procedure is why is the next find successful when the oRng is collapsed? - "oRng.Collapse wdcollapseEnd"

    Thank you so much!

  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: Find and Replace/Locating Spaces (2000/Service Pac

    If a range includes text, the Find.Execute will search within that text. If it's collapsed, the Find.Execute will search forward through the document (assuming .Forward is True) from the collapsed location.

Posting Permissions

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