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

    Search and Replace/Ranges (VBA/Word/2000)

    Hello All,

    I use ranges to search and replace text. I have macros which either apply formatting or add non-breaking spaces or non-breaking hyphens to certain text strings. I am very frustrated because the macros sometimes catch all instances of the text string and other times they do not and I can't figure out why. I am attaching a document that includes the following code and some text. I've highlighted the text in the document that my macro keeps missing. Can anyone tell me why this is happening. I've also tried the selection property and the macro still misses the highlighted text. Any help is greatly appreciated.

    Kyle

    Here is the code:
    --------------------------------
    Public Sub mm()
    DoReplace " mM ", "^smM ", False, False
    DoReplace " mM.", "^smM.", False, False
    DoReplace " mM)", "^smM)", False, False
    DoReplace " mM/", "^smM/", False, False
    DoReplace " mM,", "^smM,", False, False

    DoReplace " Mm ", "^sMm ", False, False
    DoReplace " Mm.", "^sMm.", False, False
    DoReplace " Mm)", "^sMm)", False, False
    DoReplace " Mm/", "^sMm/", False, False
    DoReplace " Mm,", "^sMm,", False, False
    DoReplace " Mm;", "^sMm;", False, False

    DoReplace " mm ", "^smm ", False, False
    DoReplace " mm/", "^smm/", False, False
    DoReplace " mm.", "^smm.", False, False
    DoReplace " mm" & ChrW(7), "^smm", False, False
    DoReplace " mm", "^smm", False, False
    End Sub
    ---------------------------
    Private Sub DoReplace(sSearch As String, sReplace As String, _
    bSuper As Boolean, bItalic As Boolean)
    Dim oRange As Range
    Set oRange = ActiveDocument.Range

    With oRange.Find
    .ClearAllFuzzyOptions
    .ClearFormatting
    .MatchCase = True
    .MatchWholeWord = False
    .Text = sSearch
    .Replacement.Text = sReplace
    .Replacement.Font.Superscript = bSuper
    .Replacement.Font.Italic = bItalic
    .Execute Replace:=wdReplaceAll, Wrap:=wdFindContinue
    End With

    If Not (oRange Is Nothing) Then Set oRange = Nothing
    End Sub
    ----------------------

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

    Re: Search and Replace/Ranges (VBA/Word/2000)

    The character that looks like a M (in the middle of <span style="background-color: #FFFF00; color: #000000; font-weight: bold">2.5 Mm,</span hi> is not in fact a capital M. I'm not sure what it is, but capital M has an ASCW code of 77 (hex 4D) this character has a value of 924 (hex 39C)

    StuartR

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

    Re: Search and Replace/Ranges (VBA/Word/2000)

    Wow, thank you. How is this possible? How could two seemingly identical characters have differenct asc values? I thought when Word applied a font to text that the asc values would be converted to the appropriate font?

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

    Re: Search and Replace/Ranges (VBA/Word/2000)

    The character with code hex 39C is the Greek capital M (named mu); it looks like the western M. Unicode has different character sets for other languages; some characters in these sets will look exactly like standard characters but will still have a different code.

Posting Permissions

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