Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 988
  • Last Modified:

Typing accented characters in a textbox

    I am working with a textbox in Word VBA. I want to simplify typing accented letters in the box. For example, to type “é” I want the user to type “e” and then an apostrophe, which I want to make add an accent to the “e” to make it “é”.

How can I:
     a)      Know when the user has typed an apostrophe; would the KeyDown event help here?
     b)      When he has typed an apostrophe, see what the immediately previous character in the text is;
     c)      If the previous character is an “e”, then
          a.      Delete the “e”
          b.      Type a Chr(130): é
          c.      Not process the apostrophe typed.

     If I can find out how to do that, I can do the same for all the other accented characters, upper and lower case.
     Thanks!
     JRA
0
JohnRobinAllen
Asked:
JohnRobinAllen
  • 7
  • 3
  • 2
1 Solution
 
JohnRobinAllenAuthor Commented:
I should add on the above question that the user will be typing French, which will not normally have a "e" followed by an apostrophe, so the "e" + "'" sequence can be used for creating an é.
     JRA
0
 
Martin LissRetired ProgrammerCommented:
That's not a practical approach. What if the user tried to type 'She's walking' which you would translate as
'shés walking'.

I just realized that I'm using an English example which may not apply but there are other ways to do it like a right-click popup from which the user could select the accented letter.
0
 
GrahamSkanRetiredCommented:
Hi JRA,

By textbox, do you mean an ActiveX control or a Word document textbox?

In the case of the latter, the only immediate action that can be done is via Autocorrect. There isn't a way of programatically monitoring the typing to a document (no typing events are raised).

Doing it via Autocorrect would mean that you can't confine it to the Textbox, but it might be your best way, anyway.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
JohnRobinAllenAuthor Commented:
Thanks for your interest. I want to make the process as simple as possible. Users writing in French will think "e acute" and typing those two keystrokes with an apostrophe for the acute accent is very fast and easy.

That same thinking process happens with all the accented characters, such as ç as c cedilla (c + ,): the letter first, then the accent. You also will have no "c," in French.

     Years ago I did this process with the MS PDS (Professional Development System) for Basic, but VBA twenty years later is a new ballgame

     JRA
0
 
JohnRobinAllenAuthor Commented:
I hesitate to disagree with Graham Skan. I am using a Word Textbox. Can one not revise the text in a textbox on the fly between keystrokes? --JRA
0
 
GrahamSkanRetiredCommented:
No, really. AFAIK, Autocorrect is the only way of doing that, but we're all willing to learn, and I'd love to hear differently. It would open up a whole new world of possibilities.

It sounds surprising, to those used to full WinForm interactivity, but the objective of  Word is to produce a well-formatted (lines folding correctly, page numbers correct, page breaks where necessary, etc) document for printing.

The Word development team did create a way of adding ActiveX controls on to the document, but they are heavy resource users, and don't look very good on the printed document.
0
 
JohnRobinAllenAuthor Commented:
I will work on it, but if you or anyone else has suggestions, that would help me to arrive at a solution. My original query stated a number of small questions. Could you or anyone else answer some of those questions? I suspect that keypress will give me the character typed. There must be some way I can know the cursor location in the text, and if so, I could then pull out the letter immediately to the left. The problem then would be to overwrite that character with what I want.
     This worked very well for me years ago. It should still do so with a bit of tweaking.
     Thanks for your interest.

     JRA
0
 
Martin LissRetired ProgrammerCommented:
Replacing the two characters with one would be no problem; just use Replace with a Mid statement isolating the two characters and replace them with the new character.

You can use Text.SelStart to know where the cursor is.
0
 
GrahamSkanRetiredCommented:
Young Martin (:>),

   Text.SelStart is good for ActiveX textboxes, but is irrelevant for Word document textboxes. If we were talking about the former, we could use the Change or KeyDown events to trigger the editing procedures.
0
 
JohnRobinAllenAuthor Commented:
I hope to get back to this problem in a week or two, but right now I have other things that have to be done. I am sure the problem can be solved by using KeyDown or KeyPress to find out what key was pressed. If it is an apostrophe, then one can see what the preceding character in the text is. If it is an "e", then one would replace it with a chr(130), i.e., "é". One would probably also have to avoid having the apostrophe be printed.
     If anyone else wants to work that sub out, I would be most grateful, and the problem would be solved. Otherwise I will get back to it in a week or so.
        JRA
0
 
JohnRobinAllenAuthor Commented:
Thanks to Graham Skan's answer in another query, I now have a potential solution for this problem. See the attached code. This presumes that the textbox is named tbAns. The code is useful if one is typing French where one thinks "é" is "e-acute". (In other words, one first names the letter and then the accent.) One simply types things in that order: first the "e" then the acute (i.e., apostrophe). The attached code has more details.

     I would like to be able to send a flag to the textbox when I call it so that I could turn off this code when the user will be typing English. Inside the module that calls the text box in a form, I will know what language the person is using. How can I specify the language from the calling module to set some variable that the KeyPress sub will be able to access to determine whether to run the keyboard changing?

    --JRA
Private Sub tbAns_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'     Allows one to type French accented characters in a textbox. One types the letter that takes an accent first, then
'     one types the accent, which changes the unaccented letter into an accented letter.
'           To type an acute accent, use the apostrophe. Thus "e'" becomes "é"
'           To type a grave accent use the grave key in the upper left corner of the American/English keyboard
'           To type a circumflex accent, use Shift-6, which is the circumflex accent. If you forget to press the shift key,
'                 it still puts a circumflex over the letter.
'           To type a cedilla, use a comma.
'     These changes assume you are typing French, which will never have an e followed by an apostrophe or a c followed by a comma.

Dim NewLetter As String
Dim TextInBox As Variant
Dim Where1 As Integer
      With tbAns
            Select Case KeyAscii
                  Case Is = 39      '     apostrophe; make e' become é
                        Select Case Asc(Mid(.Text, .SelStart))
                              Case Is = 101     '     "e"
                                    NewLetter = "é"
                                    GoSub MakeChange
                              Case Is = 69      '     "E"
                                    NewLetter = "É"
                                    GoSub MakeChange
                        End Select
                  Case Is = 44, 60       '     comma: make "c," become "ç" and "C," become "Ç"; accidental Shift-comma (60)does same
                        Select Case Asc(Mid(.Text, .SelStart))
                              Case Is = 99     '     "c"
                                    NewLetter = "ç"
                                    GoSub MakeChange
                              Case Is = 67      '     "C"
                                    NewLetter = "Ç"
                                    GoSub MakeChange
                        End Select
                  Case 94, 54      '     circonflex for â, ê, î, ô, û and uppercase variants
                        Select Case Asc(Mid(.Text, .SelStart))    '     Circumflex cases
                              Case Is = 97     '     "a"
                                    NewLetter = "â"
                                    GoSub MakeChange
                              Case Is = 65      '     "A"
                                    NewLetter = "Â"
                                    GoSub MakeChange
                              
                              Case Is = 101     '     "e"
                                    NewLetter = "ê"
                                    GoSub MakeChange
                              Case Is = 69      '     "E"
                                    NewLetter = "Ê"
                                    GoSub MakeChange
                              
                              Case Is = 105     '     "i"
                                    NewLetter = "î"
                                    GoSub MakeChange
                              Case Is = 73      '     "I"
                                    NewLetter = "Î"
                                    GoSub MakeChange
                              
                              Case Is = 111     '     "o"
                                    NewLetter = "ô"
                                    GoSub MakeChange
                              Case Is = 79      '     "O"
                                    NewLetter = "Ô"
                                    GoSub MakeChange
                              
                              Case Is = 117     '     "u"
                                    NewLetter = "û"
                                    GoSub MakeChange
                              Case Is = 85      '     "U"
                                    NewLetter = "Û"
                                    GoSub MakeChange
                        End Select        '     Balances Select Case Asc(Mid(.Text, .SelStart))    '     Circumflex cases
                  Case Is = 96      '     grave accent for à, è, ù and uppercase variants
                        Select Case Asc(Mid(.Text, .SelStart))    '     Circumflex cases
                              Case Is = 97     '     "a"
                                    NewLetter = "à"
                                    GoSub MakeChange
                              Case Is = 65      '     "A"
                                    NewLetter = "À"
                                    GoSub MakeChange
                              
                              Case Is = 101     '     "e"
                                    NewLetter = "è"
                                    GoSub MakeChange
                              Case Is = 69      '     "E"
                                    NewLetter = "È"
                                    GoSub MakeChange
                        
                              Case Is = 117     '     "u"
                                    NewLetter = "ù"
                                    GoSub MakeChange
                              Case Is = 85      '     "U"
                                    NewLetter = "Ù"
                                    GoSub MakeChange
                        End Select  '     Balances Select Case Asc(Mid(.Text, .SelStart))    '     Circumflex cases
            End Select              '     Balances Select Case KeyAscii
            'KeyAscii = 65
      'TextBox1.Text = TextBox1.Text & Chr(KeyAscii)
      
      End With                      '     Balances With TextBox1
      Exit Sub

MakeChange:
      With tbAns
            Where = .SelStart                               '     Yes, so see where we are
            TextInBox = .Text                               '     Work with a copy of the text
            Mid(TextInBox, Where, 1) = NewLetter
            tbAns.Text = TextInBox
            .SelStart = Where
            KeyAscii = 0
      End With
      Return

End Sub

Open in new window

0
 
JohnRobinAllenAuthor Commented:
I do not want these points. I just want to make this question be "solved," and I do not know how to do that without accepting my own solution.
    --JRA
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now