none
VBA: capturing symbols/special characters in the selected text RRS feed

  • Question

  •  

    Could anybody please help me out?

     

    Problem to do:

    • The user selects a range of text in Word and launches my macro
    • The text contains a few right-arrow symbols (not any arrow, just the kind of arrow that automatically appears in place when you type --> in Word)
    • My macro takes the selected string, replaces each such arrow with a [ArrowSymbol] tag, and then outputs the text to where I need it.

     

    Example:

    Selected text: A-->B-->C

    Expected output: A[ArrowSymbol]B[ArrowSymbol]C

     

    Difficulties:

    • The instr() function doesn't spot this arrow, no matter whether I refer to it using "ChrW(-3872)" or other stuff. It looks like it's interpreted as a chr(40), the left parenthesis.
    • Selection.find doesn't solve my problem because I don't want to manipulate the selected range in Word (I don't want to modify the user's selection). Instead I want to capture the text but with those symbols replaced with my tags.

     

    I'm looking forward to your kind help. Many thanks in advance.

    Tuesday, September 18, 2012 7:40 AM

All replies

  • Hi my identical named friend ;]
    In VBA you can step throw 1 sign using mid() function and asc() to find number of chr().

    Take look on that:

    Sub ASC_to_Chr()
    Const s$ = "Oskar"
    Dim x&, w$
    For x = 1 To Len(s)
        w = w & Mid(s, x, 1) & "=" & Asc(Mid(s, x, 1)) & ","
    Next x
    Debug.Print w 'in immediate window Ctrl+G
    End Sub

    to use --> you can take chr(45) & chr(45) & chr(62)

    or ChrW(2192) but that depends of Font.


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Tuesday, September 18, 2012 8:40 AM
    Answerer
  •  Hi Oskar,

    Thank you for your kind help.

    But my issue is not capturing a character in a string. It's about capturing the special character, the arrow that appears when the user inputs --> (two dashes and a greater-than). The symbol that automatically shows up seems not to be any character the chrw() function can represent.

    I actually tried the method you suggested. It returns an ascii code of 40, which corresponds to the left parenthesis ( . But I don't want to capture left parentheses, but that special rightward arrow.

     

    Thanks a lot!

    Tuesday, September 18, 2012 9:59 AM
  • Every thing is fine - method is good, but as I said "depends of Font" your using.
    When you copy sign from Character map, your font is copy too (beside of his no).

    Try this one on empty wks

    Sub test()
    With Range("a1")
        .Value = "Symbol font"
        .Offset(, 1).Value = "Your font"
        With .Offset(1)
            .Value = ChrW(174) ' ->
            .Offset(, 1).Value = ChrW(174)
            .Font.Name = "Symbol"
        End With
    End With
    End Sub


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Tuesday, September 18, 2012 10:50 AM
    Answerer
  • Hi Oskar,

    Sorry, I'd bother you again.

    1) Is this example code for Excel? Then could you also give a solution for my case, which is in Word?

    2) I don't have a problem inserting symbols into a Word doc. My problem is with reading a string containing symbols and identifying the symbols within.

    thank you so much for your help!

    Tuesday, September 18, 2012 3:11 PM
  • ok - no problem - I do not use Word every day, and you do not write application your used

    This is sample of searching and add arrow (same as in Excel)

    Fig: Before\After

    and code:

    Sub Put_follow_in_front_of_word()
    'MVP OShon from VBATools.pl
    With Selection
        With .Find
            .ClearFormatting
            .MatchWholeWord = True
            .MatchCase = True
            .Execute FindText:="something" '<- searching word
        End With
        .HomeKey Unit:=wdLine '<- to begining of line
        .Text = Chr(174)      '<- put arrow in character your font
        .Font.Name = "Symbol" '<- change font to see this arrow
    End With
    End Sub


    and that what your asking for A->B->C sample:


    Sub blablabla()
    Call Put_follow_in_front_of_word("A")
    Call Put_follow_in_front_of_word("B")
    End Sub
    
    Sub Put_follow_in_front_of_word(ftext$)
    With Selection
        .HomeKey Unit:=wdStory '<- et start your document
        With .Find
            .ClearFormatting
            .MatchWholeWord = True
            .MatchCase = True
            .Execute FindText:=ftext '<- searching word
        End With
        .MoveRight Unit:=wdCharacter, Count:=1  '<- one step to left
        .Text = Chr(174)      '<- put arrow in character your font
        .Font.Name = "Symbol" '<- change font to see this arrow
    End With
    End Sub


    Regards ;]



    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved



    Tuesday, September 18, 2012 5:01 PM
    Answerer
  • I'd appreciate your patience, Oskar.

    But my situation is that the "selection.find" method doesn't solve my problem (it was one of the solutions i tried previously).

    • "Selection.find doesn't solve my problem because I don't want to manipulate the selected range in Word (I don't want to modify the user's selection). Instead I want to capture the text but with those symbols replaced with my tags."

    It's not because I don't want to use it, but because what I want to do is to replace (not in the Word doc but in my string variable) all arrows with [ArrowSymbol] tags and output the result string. But I'm not supposed to "touch" the original text in Word (for example, by doing a replace in the Word doc and then copying the result text; i can definitely NOT do this.). I need only read the string containing symbols and search for those symbols within the string.

    By the way, I only want to get the selected text (with certain symbols replaced), not the whole doc. Thanks a lot!
    • Edited by Oscar Q Zhao Wednesday, September 19, 2012 12:18 AM
    Wednesday, September 19, 2012 12:13 AM
  • Maybe I have lang barrier, because i do not follow you.

    That sounds like you want to use auto-correct during write, do you?

    Do not call any procedure, but make automate class changing what user writes.

    Gimme some real sample or describe little more about conditions, which the code should to operate.


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Wednesday, September 19, 2012 7:24 AM
    Answerer
  • Sorry, I didn't make my purpose very clear.

    Instead of doing an auto-correction, I want to "parse" the selected text and convert it to a text string.

      • The user selects any text in Word and then runs my macro.
      • The macro reads the selected text.
      • The macro outputs the selected text (maybe in a popup), but with each occurrence of that symbol (that rightward arrow) replaced with a tag called[ArrowSymbol].

    • Proposed as answer by BigMeanCat Thursday, September 20, 2012 7:56 PM
    • Unproposed as answer by BigMeanCat Thursday, September 20, 2012 7:56 PM
    Thursday, September 20, 2012 1:20 AM
  • How something like this

    Public Sub test()
        x = Selection.Text
        y = Replace(x, Chr(40), "[ArrowSymbol]")
    End Sub

    Thursday, September 20, 2012 7:57 PM
  • Try ChrW(8594). I think what's happening is that for Wingdings (the font that the arrow is in), Word wants you to use the decimal unicode value rather than its internal negative character number.

    Saturday, September 22, 2012 8:31 PM
  • Hi BigMeanCat, thank you for your suggestion. I tried this when i got started, but this would unexpectedly replace "(" in my original text.
    Monday, September 24, 2012 12:56 AM
  • Sorry, LinusCMH, i gave it a try, but neither instr() nor replace() interpreted that arrow symbol as ChrW(8594).
    Monday, September 24, 2012 12:58 AM