Results 1 to 3 of 3
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Sub cmd_DisplayAllCharacterCode()

    I used to think that my original WordBasic "Display Character Code" was pretty neat, the way it displayed the selected chracter code and then moved along one character to the right.

    Years pass.

    This morning I wondered why I didn't display a STRING of character codes for all selected characters.

    Now I do.

    <pre>Public Sub cmd_DisplayAllCharacterCode()
    ' Procedure : cmd_DisplayAllCharacterCode
    ' Description: Displays the ascii character code of the selected string.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: None
    ' Returns: Nothing
    ' Assumes: Nothing
    ' Side Effects: The selection may change.
    ' Tested: By a call from the user.
    Dim strText As String
    strText = Selection.Text
    Dim strCodes As String
    strCodes = ""
    Dim i As Integer
    For i = 1 To Len(strText)
    strCodes = strCodes & " " & intCharacterCode(Mid$(strText, i, 1))
    Next i
    Application.StatusBar = strCodes
    End Sub
    </pre>


    Here's the intCharacterCode, for which you should thank Rajesh.

    <pre>Public Function intCharacterCode(strCh As String) As Integer
    ' Procedure : intCharacterCode
    ' Description: Return the Unicode of the character.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: STRING
    ' Returns: INTEGER
    ' Assumes: Nothing
    ' Side Effects: The selection may change.
    ' Tested: By a call from the user
    Dim intUniCode As Integer
    intUniCode = AscW(strCh)
    If intUniCode < 0 Then intUniCode = intUniCode + 65536
    intCharacterCode = intUniCode
    'Sub TESTintCharacterCode()
    'MsgBox intCharacterCode("*")
    'End Sub
    End Function
    </pre>


  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Code: Sub cmd_DisplayAllCharacterCode()

    Chris,

    This looks neat. Just a couple of comments:

    If intUniCode is an Integer and you add 65536 to it, won't that cause an error.

    You probably don't need the strCode = "" statement - I think when you declare a string variable it initializes an empty string, so the second statement is redundant.

    The Sub says it displays the ascii code, but the function says it returns the unicode <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>?

    Gary

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Sub cmd_DisplayAllCharacterCode()

    Gary, thanks for your reply, just a few comments:

    1) A couple of comments would be two comments, right? (GRIN!)

    > If intUniCode is an Integer and you add 65536 to it, won't that cause an error.

    Blame Rajesh for this. I suspect the answer is that high-ascii character codes appear as negative numbers in Integer format?


    > You probably don't need the strCode = "" statement

    In VBA you are probably right. Except that I've been cuaght so many times, I try to do it all the time. Like using my turn-signals whene there's no-one around. Watch them change VBA a year from now and watch all the code collapse. No?


    > The Sub says it displays the ascii code, but

    Right. A poor effort at the description on my aprt.

Posting Permissions

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