?
Solved

In a textbox, prevent certain typed characters from appearing. (Word and VBA)

Posted on 2011-10-19
4
Medium Priority
?
396 Views
Last Modified: 2012-08-13
    Using VBA with Word, I have a textbox and want to block certain characters typed so that they do not appear in the text. Specifically I want to put into the KeyPress Sub code that will intercept an apostrophe, check the letter to the left of where the apostrophe will go in the text, and if it is an “e” I want to replace that character with an “é”. In other words, I want to change “e'” to “é”. I have code that will do that now if the insertion point is at the end of the words being written. I hope soon to be able to do that when the insertion point is elsewhere in the text. What I cannot figure out is that after I have made the change, how do I get the apostrophe not to appear after the revised letter?

     Thanks for any help. If I can get this solution, I can simplify the typing of accented letters in a textbox.

     JRA
0
Comment
Question by:JohnRobinAllen
  • 3
4 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36993689
I did do something like this quite a while ago, to prevent certain characters being entered - I think the thing is to change KeyAscii to 0 if it's a character you don't want to enter - this is my validation code.  I'm not sure exactly what code you have to enter an accented e, but this may help:
Private Sub mctlTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
    Const cstrNumbers As String = "0123456789"
    Const cstrNumbersPlus As String = "+-."
    Const cstrAlpha As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Const cstrSpace As String = " "
    Dim strValid As String
        Select Case meValidation

        Case NumberOnly
            strValid = cstrNumbers
            
        Case NumberPlusMinusDecimal
            strValid = cstrNumbers & cstrNumbersPlus
            
        Case Alpha
            strValid = cstrAlpha

        Case AlphaSpace
            strValid = cstrAlpha & cstrSpace
            
        Case AlphaNumeric
            strValid = cstrAlpha & cstrNumbers

        Case AlphaNumericSpace
            strValid = cstrAlpha & cstrNumbers & cstrSpace

        Case Else
            Exit Sub
    End Select
    
    If InStr(1, strValid, Chr$(KeyAscii)) = 0 Then
        KeyAscii = 0
    End If
    
End Sub

Open in new window

0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 800 total points
ID: 36993792
Just playing quickly with that idea, this seems to do what you want (obviously just for accented lower case e, but the principle should be the same for others:
Private Sub tbName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim x As Long
    Dim strTemp As String
    
    If Chr$(KeyAscii) = "'" Then
        x = Me.tbName.SelStart
        
        If x > 0 Then
            strTemp = Me.tbName.Value
            If Mid$(strTemp, x, 1) = "e" Then
                Mid(strTemp, x, 1) = Chr$(&HE9)
                Me.tbName.Value = strTemp
                Me.tbName.SelStart = x
                KeyAscii = 0
            End If
        End If
    End If
    
End Sub

Open in new window

0
 

Author Closing Comment

by:JohnRobinAllen
ID: 36995715
I certainly wish I knew more about VBA, but your solution works. The first code used the term meValidation, and I could not find anything about that in MS Help. What I needed was to know how to kill a key that was pressed, and setting the KeyAscii to zero is the solution.
     The second bit of code was something I needed a couple of months ago. I finally encoded that and posted it a couple of minutes ago in another query. My solution was quite close to the one you submitted. I still have a few kinks to smooth out, but I am hopeful they will get solved soon.
     Thanks for all your help. You gave a lot more than I had asked for, and I am grateful.
     JRA
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36996082
Yes - sorry, my first example is a snippet from a larger piece of code I have developed to get some sort of proper validation for VBA text boxes.  meValidation is just an enum I have defined to enumerate different types of validation and the idea is that for each type, only the valid characters can be entered.  In fact I don't use validation that strict very often, but there is other code to do numeric only and date checking which is often useful.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question