none
VBA Code to find degree symbol in Unicode Hex RRS feed

  • Question

  • Hi,

    I am trying to develop a macro to find and replace some characters in word.

    I have the below code which can work to achieve it. However the ChrW for degree in Unicode Hex is "00B0" and the VBA editor tends to delete the leading 0 in the code and leaves only "B0".

    Can someone help me with retaining the ChrW as "00B0" in the editor.

    Below is the code:

    Sub FindReplaceSymbols()

     With rng.Find

            .Text = ChrW(&H00B0)

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3920, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

    End Sub

    Tuesday, January 10, 2017 3:55 PM

Answers

  • Hi mayurthakor13,

    when you directly pass the &H00B0 then editor will remove the 00 before the B0.

    so it looks like &HB0.

    but you want to keep it as you enter.

    so you just need to pass it between double quotes like below.

    "&H00B0".

    it will not remove 0 after that and work correctly.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mayurthakor13 Wednesday, January 11, 2017 5:13 PM
    Wednesday, January 11, 2017 12:39 AM
    Moderator
  • Hi mayurthakor13,

    when you directly pass the &H00B0 then editor will remove the 00 before the B0.

    so it looks like &HB0.

    but you want to keep it as you enter.

    so you just need to pass it between double quotes like below.

    "&H00B0".

    it will not remove 0 after that and work correctly..

    Do be aware that the deletion of the leading 0s is of no consequence.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Wednesday, January 11, 2017 5:14 PM
    Wednesday, January 11, 2017 8:42 AM
  • It's difficult to know from your post whether some of the content you're trying to Find (i.e. ><°µ+±) is Symbol content or normal text (note that the degree character - ° - doesn't need to be expressed in ChrW terms). Assuming the latter, you should be able to use something like:

    Sub FindReplaceSymbols()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[\>\<°µ+±" & ChrW(&H3B1) & ChrW(&H3B2) & ChrW(&H2264) & ChrW(&H2265) & "]"
        .MatchWildcards = True
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case ">": .InsertSymbol Font:="Symbol", CharacterNumber:=62, Unicode:=True
          Case "<": .InsertSymbol Font:="Symbol", CharacterNumber:=60, Unicode:=True
          Case "°": .InsertSymbol Font:="Symbol", CharacterNumber:=176, Unicode:=True
          Case "µ": .InsertSymbol Font:="Symbol", CharacterNumber:=109, Unicode:=True
          Case "+": .InsertSymbol Font:="Symbol", CharacterNumber:=43, Unicode:=True
          Case "±": .InsertSymbol Font:="Symbol", CharacterNumber:=177, Unicode:=True
          Case ChrW(&H2265): .InsertSymbol Font:="Symbol", CharacterNumber:=179, Unicode:=True
          Case ChrW(&H2264): .InsertSymbol Font:="Symbol", CharacterNumber:=163, Unicode:=True
          Case ChrW(&H3B1): .InsertSymbol Font:="Symbol", CharacterNumber:=97, Unicode:=True
          Case ChrW(&H3B2): .InsertSymbol Font:="Symbol", CharacterNumber:=98, Unicode:=True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Thursday, January 12, 2017 8:37 AM
    Thursday, January 12, 2017 5:53 AM
  • Try:

    Sub FindReplaceSymbols()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = True
        .Forward = True
        .Format = False
        .Wrap = wdFindContinue
        'Remove spaces after the symbols <, >, ±, =<, =>, =
        .Text = "([\>\<±" & ChrW(&H2264) & ChrW(&H2265) & "])[ ^s]{1,}"
        .Replacement.Text = "\1"
        .Execute Replace:=wdReplaceAll
        'Remove spaces before the symbols <, >, °, ±, =<, =>, =, %, ™, ©, ®
        .Text = "[ ^s]{1,}([\>\<°±%™©®" & ChrW(&H2264) & ChrW(&H2265) & "])"
        .Execute Replace:=wdReplaceAll
        'Re-insert single spaces before & after the symbols <, >, ±, =<, =>, =
        .Text = "[\>\<±=" & ChrW(&H2264) & ChrW(&H2265) & "]"
        .Replacement.Text = " ^& "
        .Execute Replace:=wdReplaceAll
        .Wrap = wdFindStop
        .Text = "[\>\<°µ+±#%=™©®~" & ChrW(&H3B1) & ChrW(&H3B2) & ChrW(&H3B3) & ChrW(&H3BB) & ChrW(&H2264) & ChrW(&H2265) & "]"
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case "#": .InsertSymbol Font:="Symbol", CharacterNumber:=35, Unicode:=True
          Case "%": .InsertSymbol Font:="Symbol", CharacterNumber:=37, Unicode:=True
          Case "+": .InsertSymbol Font:="Symbol", CharacterNumber:=43, Unicode:=True
          Case "<": .InsertSymbol Font:="Symbol", CharacterNumber:=60, Unicode:=True
          Case "=": .InsertSymbol Font:="Symbol", CharacterNumber:=61, Unicode:=True
          Case ">": .InsertSymbol Font:="Symbol", CharacterNumber:=62, Unicode:=True
          Case ChrW(&H3B1): .InsertSymbol Font:="Symbol", CharacterNumber:=97, Unicode:=True
          Case ChrW(&H3B2): .InsertSymbol Font:="Symbol", CharacterNumber:=98, Unicode:=True
          Case "~": .InsertSymbol Font:="Symbol", CharacterNumber:=126, Unicode:=True
          Case "µ": .InsertSymbol Font:="Symbol", CharacterNumber:=109, Unicode:=True
          Case ChrW(&H2264): .InsertSymbol Font:="Symbol", CharacterNumber:=163, Unicode:=True
          Case "°": .InsertSymbol Font:="Symbol", CharacterNumber:=176, Unicode:=True
          Case "±": .InsertSymbol Font:="Symbol", CharacterNumber:=177, Unicode:=True
          Case ChrW(&H2265): .InsertSymbol Font:="Symbol", CharacterNumber:=179, Unicode:=True
          Case "®": .Font.Superscript = True: .Font.Size = 11: .InsertSymbol Font:="Symbol", CharacterNumber:=226, Unicode:=True
          Case "©": .Font.Superscript = True: .Font.Size = 11: .InsertSymbol Font:="Symbol", CharacterNumber:=227, Unicode:=True
          Case "™": .Font.Superscript = True: .Font.Size = 11: .InsertSymbol Font:="Symbol", CharacterNumber:=228, Unicode:=True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    Do note that the symbols ™, ©, ® are already positioned above the font baseline, so superscripting really shouldn't be necessary.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Tuesday, January 24, 2017 9:35 AM
    Monday, January 23, 2017 9:24 AM
  • Change:
    .Replacement.Text = " ^& "
    to:
    .Replacement.Text = "^s^&^s"

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Tuesday, January 31, 2017 3:13 AM
    Tuesday, January 24, 2017 10:32 AM

All replies

  • You're already using ChrW(&H00B0), which is the correct way to express hexadecimal values. In any event, the usual character for degree symbols is ASCII 186, which you could find with:
    .Text = "^0176"
    (which is the same as Unicode 00B0 and becomes ChrW(&HB0)).


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, January 11, 2017 8:44 AM Typo
    Tuesday, January 10, 2017 9:28 PM
  • Hi mayurthakor13,

    when you directly pass the &H00B0 then editor will remove the 00 before the B0.

    so it looks like &HB0.

    but you want to keep it as you enter.

    so you just need to pass it between double quotes like below.

    "&H00B0".

    it will not remove 0 after that and work correctly.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mayurthakor13 Wednesday, January 11, 2017 5:13 PM
    Wednesday, January 11, 2017 12:39 AM
    Moderator
  • Hi mayurthakor13,

    when you directly pass the &H00B0 then editor will remove the 00 before the B0.

    so it looks like &HB0.

    but you want to keep it as you enter.

    so you just need to pass it between double quotes like below.

    "&H00B0".

    it will not remove 0 after that and work correctly..

    Do be aware that the deletion of the leading 0s is of no consequence.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Wednesday, January 11, 2017 5:14 PM
    Wednesday, January 11, 2017 8:42 AM
  • Thanks Deepak. This is good to know
    Wednesday, January 11, 2017 5:13 PM
  • Thanks Paul. I realized that when I run the code (which also contains code for other symbols) the degree symbol did not change but when I run it the second time it changed. So the disappearing of 0 was not the cause. God help me now!
    Wednesday, January 11, 2017 5:18 PM
  • I realize disappearing of 0 was not an issue for my program to run. What I am trying to achieve is I have some symbols in a word document in "normal text" font which I want to find and replace to "Symbol" font. I have a sample word document which contains list of symbols in "normal text" font in the second column and in "Symbol" font in the third column. Unfortunately I am not able to attach the word file here. I am using the below code for this task.

    Sub FindReplaceSymbols()

    '

        Dim rng As Range

        Set rng = ActiveDocument.Range

        With rng.Find

            .Text = ">"

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-4034, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = "<"

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-4036, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = ChrW("&H00B0")

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3920, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = ChrW(&H2265)

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3917, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = ChrW(&H2264)

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3933, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = "µ"

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3987, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = "+"

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-4053, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = "±"

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3919, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = ChrW(&H3B1)

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3999, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With

        With rng.Find

            .Text = ChrW(&H3B2)

            .Forward = True

            Do While .Execute

                rng.InsertSymbol Font:="Symbol", CharacterNumber:=-3998, Unicode:=True

                rng.Collapse wdCollapseEnd

                rng.End = ActiveDocument.Range.End

            Loop

        End With   

    End Sub

    After running this code what I observed is, it changes only some symbols when it was run for the first time. And all the symbols are changed when its run for 3 times. This dosen't give the desired output as my actual live documents contain each symbol more than 50 times at the least and running the program for more number of times does not serve the purpose of macro. Can someone please help me with this?



    Wednesday, January 11, 2017 5:44 PM
  • Hi mayurthakor13,

    I try to look in to your code mentioned in your last post.

    you had mentioned that when you run the code for 3rd time then it replaces all the symbols.

    first of all I want to say that why you use so many loops. individual loop for each symbol.

    it will just increase the length of the code.

    instead of that try to use only 1 loop.

    try to save all the symbol in array and pass it in loop.

    here you need to change the logic.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 12, 2017 5:29 AM
    Moderator
  • It's difficult to know from your post whether some of the content you're trying to Find (i.e. ><°µ+±) is Symbol content or normal text (note that the degree character - ° - doesn't need to be expressed in ChrW terms). Assuming the latter, you should be able to use something like:

    Sub FindReplaceSymbols()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[\>\<°µ+±" & ChrW(&H3B1) & ChrW(&H3B2) & ChrW(&H2264) & ChrW(&H2265) & "]"
        .MatchWildcards = True
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case ">": .InsertSymbol Font:="Symbol", CharacterNumber:=62, Unicode:=True
          Case "<": .InsertSymbol Font:="Symbol", CharacterNumber:=60, Unicode:=True
          Case "°": .InsertSymbol Font:="Symbol", CharacterNumber:=176, Unicode:=True
          Case "µ": .InsertSymbol Font:="Symbol", CharacterNumber:=109, Unicode:=True
          Case "+": .InsertSymbol Font:="Symbol", CharacterNumber:=43, Unicode:=True
          Case "±": .InsertSymbol Font:="Symbol", CharacterNumber:=177, Unicode:=True
          Case ChrW(&H2265): .InsertSymbol Font:="Symbol", CharacterNumber:=179, Unicode:=True
          Case ChrW(&H2264): .InsertSymbol Font:="Symbol", CharacterNumber:=163, Unicode:=True
          Case ChrW(&H3B1): .InsertSymbol Font:="Symbol", CharacterNumber:=97, Unicode:=True
          Case ChrW(&H3B2): .InsertSymbol Font:="Symbol", CharacterNumber:=98, Unicode:=True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Thursday, January 12, 2017 8:37 AM
    Thursday, January 12, 2017 5:53 AM
  • Thank you Paul. This is awesome!!
    Thursday, January 12, 2017 8:38 AM
  • Hello guys,

    There is one more situation in the above case. The symbols for Trade mark "ä" (Character number=228) and Registered Mark "â" (Character number=226) should be in superscript. When I used the above code for these symbols only the font was changed to "Symbol" style. To make them superscript I used the function ".Font.Superscript = True" however it did not change to superscript. Can you guys please modify the below code to add the superscript for these two symbols.

    Also is it possible to have spaces added/removed around the symbols?

    For the symbols %, ° there should be no spaces.

    For eg,

    "21 ° C", "21° C", "21 °C" should be "21°C" (no spaces around the symbol)

    "25 %" should be "25%" (no space before symbol)

    And for the symbols =, ±, ≥, ≤, <, > there should be a space before and after the symbol

    For eg,

    "10±3" should be "10 ± 3"

    "≥60" should be " ≥ 60"

    Sometimes the spaces are in place as per the requirement, in that case the macro should not add any extra spaces. It would be great if this can be achieved.

    Thanks

    Sub FindReplaceSymbols()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[\>\<°µ+±#%=™©®~" & ChrW(&H3B1) & ChrW(&H3B2) & ChrW(&H3B3) & ChrW(&H3BB) & ChrW(&H2264) & ChrW(&H2265) & "]"
        .MatchWildcards = True
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case ">": .InsertSymbol Font:="Symbol", CharacterNumber:=62, Unicode:=True
          Case "<": .InsertSymbol Font:="Symbol", CharacterNumber:=60, Unicode:=True
          Case "°": .InsertSymbol Font:="Symbol", CharacterNumber:=176, Unicode:=True
          Case "µ": .InsertSymbol Font:="Symbol", CharacterNumber:=109, Unicode:=True
          Case "+": .InsertSymbol Font:="Symbol", CharacterNumber:=43, Unicode:=True
          Case "±": .InsertSymbol Font:="Symbol", CharacterNumber:=177, Unicode:=True
          Case "#": .InsertSymbol Font:="Symbol", CharacterNumber:=35, Unicode:=True
          Case "%": .InsertSymbol Font:="Symbol", CharacterNumber:=37, Unicode:=True
          Case "=": .InsertSymbol Font:="Symbol", CharacterNumber:=61, Unicode:=True
          Case "™": .InsertSymbol Font:="Symbol", CharacterNumber:=228, Unicode:=True
          Case "©": .InsertSymbol Font:="Symbol", CharacterNumber:=227, Unicode:=True
          Case "®": .InsertSymbol Font:="Symbol", CharacterNumber:=226, Unicode:=True
          Case "~": .InsertSymbol Font:="Symbol", CharacterNumber:=126, Unicode:=True
          Case ChrW(&H2265): .InsertSymbol Font:="Symbol", CharacterNumber:=179, Unicode:=True
          Case ChrW(&H2264): .InsertSymbol Font:="Symbol", CharacterNumber:=163, Unicode:=True
          Case ChrW(&H3B1): .InsertSymbol Font:="Symbol", CharacterNumber:=97, Unicode:=True
          Case ChrW(&H3B2): .InsertSymbol Font:="Symbol", CharacterNumber:=98, Unicode:=True
          Case ChrW(&H3B3): .InsertSymbol Font:="Symbol", CharacterNumber:=103, Unicode:=True
          Case ChrW(&H3BB): .InsertSymbol Font:="Symbol", CharacterNumber:=108, Unicode:=True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Wednesday, January 18, 2017 5:45 AM
  • Hello,

    I have a code that changes symbols in "normal text" font to "symbol" font. However I want the symbols for Registered mark "®" (Character number=228) and Trade Mark "" (Character number=226) to be in superscript if they are not already. The code that I have only changes the font. To make them superscript I used the function ".Font.Superscript = True" however it did not change to superscript. Can anyone please modify the below code to add the superscript for these two symbols.

    Also is it possible to have spaces added/removed around the symbols?

    For the symbols %, ° there should be no spaces.

    For eg,

    "21 ° C", "21° C", "21 °C" should be "21°C" (no spaces around the symbol)

    "25 %" should be "25%" (no space before symbol)

    And for the symbols =, ±, ≥, ≤, <, > there should be a space before and after the symbol

    For eg,

    "10±3" should be "10 ± 3"

    "≥60" should be " ≥ 60"

    Sometimes the spaces are in place as per the requirement, in that case the macro should not add any extra spaces. It would be great if this can be achieved.

    Thanks

    Sub FindReplaceSymbols()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[\>\<°µ+±#%=™©®~" & ChrW(&H3B1) & ChrW(&H3B2) & ChrW(&H3B3) & ChrW(&H3BB) & ChrW(&H2264) & ChrW(&H2265) & "]"
        .MatchWildcards = True
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case ">": .InsertSymbol Font:="Symbol", CharacterNumber:=62, Unicode:=True
          Case "<": .InsertSymbol Font:="Symbol", CharacterNumber:=60, Unicode:=True
          Case "°": .InsertSymbol Font:="Symbol", CharacterNumber:=176, Unicode:=True
          Case "µ": .InsertSymbol Font:="Symbol", CharacterNumber:=109, Unicode:=True
          Case "+": .InsertSymbol Font:="Symbol", CharacterNumber:=43, Unicode:=True
          Case "±": .InsertSymbol Font:="Symbol", CharacterNumber:=177, Unicode:=True
          Case "#": .InsertSymbol Font:="Symbol", CharacterNumber:=35, Unicode:=True
          Case "%": .InsertSymbol Font:="Symbol", CharacterNumber:=37, Unicode:=True
          Case "=": .InsertSymbol Font:="Symbol", CharacterNumber:=61, Unicode:=True
          Case "™": .InsertSymbol Font:="Symbol", CharacterNumber:=228, Unicode:=True
          Case "©": .InsertSymbol Font:="Symbol", CharacterNumber:=227, Unicode:=True
          Case "®": .InsertSymbol Font:="Symbol & Superscript", CharacterNumber:=226, Unicode:=True
          Case "~": .InsertSymbol Font:="Symbol", CharacterNumber:=126, Unicode:=True
          Case ChrW(&H2265): .InsertSymbol Font:="Symbol", CharacterNumber:=179, Unicode:=True
          Case ChrW(&H2264): .InsertSymbol Font:="Symbol", CharacterNumber:=163, Unicode:=True
          Case ChrW(&H3B1): .InsertSymbol Font:="Symbol", CharacterNumber:=97, Unicode:=True
          Case ChrW(&H3B2): .InsertSymbol Font:="Symbol", CharacterNumber:=98, Unicode:=True
          Case ChrW(&H3B3): .InsertSymbol Font:="Symbol", CharacterNumber:=103, Unicode:=True
          Case ChrW(&H3BB): .InsertSymbol Font:="Symbol", CharacterNumber:=108, Unicode:=True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Thursday, January 19, 2017 4:58 PM
  • Hi mayurthakor13,

    first try to select the text make it super script and then try to change it to symbol.

    above code is suggested by the macropod. so he will try to modify it for you when he will return on this post.

    till then if possible then show me what changes you had done in code.

    I will try to test it on my side.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 20, 2017 6:24 AM
    Moderator
  • What you want is possible, but you have a lot more 'special' characters than just the ones mentioned in your last post. What do you want done with the spacing for them?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, January 23, 2017 6:10 AM
  • I checked the spaces for all the symbols once again and found lot of inconsistency across the documents. For now, can we just correct the spaces for only below two symbols:

    For the symbols %, ° there should be no spaces.

    For eg,

    "21 ° C", "21° C", "21 °C" should be "21°C" (no spaces around the symbol)

    "25 %" should be "25%" (no space before symbol)

    And do not include other symbols and leave the spaces for other symbols as is. I am okay if we have a separate macro for this as the previous code includes all symbols.

    If you can just include the superscript part for Registered mark "®" (Character number=228) and Trade Mark "" (Character number=226) in previous code that would be great. Also I noticed that the font size of these two symbols was reduced to make them look superscript. Can we have these two symbols have font size 11 and superscript?

    Thanks and regards,

    Mayur


    Monday, January 23, 2017 7:03 AM
  • Try:

    Sub FindReplaceSymbols()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = True
        .Forward = True
        .Format = False
        .Wrap = wdFindContinue
        'Remove spaces after the symbols <, >, ±, =<, =>, =
        .Text = "([\>\<±" & ChrW(&H2264) & ChrW(&H2265) & "])[ ^s]{1,}"
        .Replacement.Text = "\1"
        .Execute Replace:=wdReplaceAll
        'Remove spaces before the symbols <, >, °, ±, =<, =>, =, %, ™, ©, ®
        .Text = "[ ^s]{1,}([\>\<°±%™©®" & ChrW(&H2264) & ChrW(&H2265) & "])"
        .Execute Replace:=wdReplaceAll
        'Re-insert single spaces before & after the symbols <, >, ±, =<, =>, =
        .Text = "[\>\<±=" & ChrW(&H2264) & ChrW(&H2265) & "]"
        .Replacement.Text = " ^& "
        .Execute Replace:=wdReplaceAll
        .Wrap = wdFindStop
        .Text = "[\>\<°µ+±#%=™©®~" & ChrW(&H3B1) & ChrW(&H3B2) & ChrW(&H3B3) & ChrW(&H3BB) & ChrW(&H2264) & ChrW(&H2265) & "]"
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case "#": .InsertSymbol Font:="Symbol", CharacterNumber:=35, Unicode:=True
          Case "%": .InsertSymbol Font:="Symbol", CharacterNumber:=37, Unicode:=True
          Case "+": .InsertSymbol Font:="Symbol", CharacterNumber:=43, Unicode:=True
          Case "<": .InsertSymbol Font:="Symbol", CharacterNumber:=60, Unicode:=True
          Case "=": .InsertSymbol Font:="Symbol", CharacterNumber:=61, Unicode:=True
          Case ">": .InsertSymbol Font:="Symbol", CharacterNumber:=62, Unicode:=True
          Case ChrW(&H3B1): .InsertSymbol Font:="Symbol", CharacterNumber:=97, Unicode:=True
          Case ChrW(&H3B2): .InsertSymbol Font:="Symbol", CharacterNumber:=98, Unicode:=True
          Case "~": .InsertSymbol Font:="Symbol", CharacterNumber:=126, Unicode:=True
          Case "µ": .InsertSymbol Font:="Symbol", CharacterNumber:=109, Unicode:=True
          Case ChrW(&H2264): .InsertSymbol Font:="Symbol", CharacterNumber:=163, Unicode:=True
          Case "°": .InsertSymbol Font:="Symbol", CharacterNumber:=176, Unicode:=True
          Case "±": .InsertSymbol Font:="Symbol", CharacterNumber:=177, Unicode:=True
          Case ChrW(&H2265): .InsertSymbol Font:="Symbol", CharacterNumber:=179, Unicode:=True
          Case "®": .Font.Superscript = True: .Font.Size = 11: .InsertSymbol Font:="Symbol", CharacterNumber:=226, Unicode:=True
          Case "©": .Font.Superscript = True: .Font.Size = 11: .InsertSymbol Font:="Symbol", CharacterNumber:=227, Unicode:=True
          Case "™": .Font.Superscript = True: .Font.Size = 11: .InsertSymbol Font:="Symbol", CharacterNumber:=228, Unicode:=True
        End Select
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    Do note that the symbols ™, ©, ® are already positioned above the font baseline, so superscripting really shouldn't be necessary.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Tuesday, January 24, 2017 9:35 AM
    Monday, January 23, 2017 9:24 AM
  • Thank you! That worked perfectly.

    I was wondering if we could add non breaking space instead of just space around the symbols so that the values stay on one line. I found the unicode for Non breaking space "00A0" and tried the following but it did not work.

    .Replacement.Text = "[ & ChrW(&HA0)]^&[ & ChrW(&HA0)]"

    How do we represent the non breaking space in the code? Can you please look into this

    Tuesday, January 24, 2017 9:40 AM
  • Change:
    .Replacement.Text = " ^& "
    to:
    .Replacement.Text = "^s^&^s"

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by mayurthakor13 Tuesday, January 31, 2017 3:13 AM
    Tuesday, January 24, 2017 10:32 AM
  • Thank you Paul. I tried the macro on my live documents and it is working great. It has made life simpler and also I have gained good knowledge of VBA for word. Thanks again.

    Regards,

    Mayur

    Tuesday, January 31, 2017 3:18 AM