Limiting the Amount of Text in a Macro Combo Box or Text Box (114298)
The information in this article applies to:
- Microsoft Word for Windows 6.0
- Microsoft Word for Windows 6.0a
- Microsoft Word for Windows 6.0c
- Microsoft Word for Windows 95
This article was previously published under Q114298 SUMMARY
If you create a custom dialog box, you can specify a limit to the amount of
text that can be entered into a combo box or text box by using SendMessage
(a Windows API function) with the EM_LIMITTEXT constant.
MORE INFORMATION
The preferred way to limit the length of a string entered into a combo or
text box is to use the SendMessage API function call. After you set the
focus to the desired edit control, you must send a message to the window's
message queue that will reset the text limit for the control. The argument
EM_LIMITTEXT, as the second parameter to SendMessage, will set the desired
text limit based on the value specified by the third argument. The
SendMessage function requires the following parameters for setting the text
limit
SendMessage Lib "User"(hWnd As Integer, wMsg As Integer, wParam As
Integer, lParam As String) As Long
where:
hWnd Specifies the handle to the edit control.
wParam Specifies the maximum number of bytes that can be entered.
If the user attempts to enter more characters, the edit
control beeps and does not accept the characters.
lParam Is not used.
Example
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN
RISK. Microsoft provides this macro code "as is" without warranty of any
kind, either express or implied, including but not limited to the implied
warranties of merchantability and/or fitness for a particular purpose.
Word 7.0
Declare Function GetFocus Lib "User32"() As Long
Declare Function SendMessageA Lib "User32"(hWnd As Long, wMsg As Integer,
wParam As Integer, lParam As String) As Long
Dim Shared WM_USER, EM_LIMITTEXT, TextLimit
Sub MAIN
WM_USER = 1024
EM_LIMITTEXT = WM_USER + 21
TextLimit = 4 'Sets the number of chars
Begin Dialog UserDialog 320, 144, "MyApp", .dlgfun
OKButton 10, 6, 88, 21
TextBox 10, 30, 160, 18, .TextBox1
PushButton 10, 51, 88, 21, "Push", .Push1
End Dialog
Dim dlg As UserDialog
Dialog dlg
End Sub
Function dlgfun(id$, action, wValue)
Select Case action
Case 1
DlgVisible "TextBox1", 0
Case 2
If id$ = "Push1" Then
DlgVisible "TextBox1", 1
DlgFocus "TextBox1"
hWnd = getfocus
retVal = SendMessageA(hWnd, EM_LIMITTEXT, TextLimit, "")
dlgfun = 1
End If
Case 3
Case 4
Case 5
Case 6
Case Else
End Select
End Function
Word 6.0
Declare Function GetFocus Lib "User"() As Integer
Declare Function SendMessage Lib "User"(hWnd As Integer, wMsg As
Integer, wParam As Integer, lParam As String) As Long
Dim Shared WM_USER, EM_LIMITTEXT, TextLimit
Sub MAIN
WM_USER = 1024
EM_LIMITTEXT = WM_USER + 21
TextLimit = 2 'Sets the number of chars
Begin Dialog UserDialog 320, 144, "MyApp", .dlgfun
OKButton 10, 6, 88, 21
TextBox 10, 30, 160, 18, .TextBox1
PushButton 10, 51, 88, 21, "Push", .Push1
End Dialog
Dim dlg As UserDialog
Dialog dlg
End Sub
Function dlgfun(id$, action, wValue)
Select Case action
Case 1
DlgVisible "TextBox1", 0
Case 2
If id$ = "Push1" Then
DlgVisible "TextBox1", 1
DlgFocus "TextBox1"
hWnd = getfocus
retVal = SendMessage(hWnd, EM_LIMITTEXT, TextLimit, "")
dlgfun = 1
End If
Case 3
Case 4
Case 5
Case 6
Case Else
End Select
End Function
REFERENCES
"Microsoft Software Development Kit (SDK) for Microsoft Windows operating
system version 3.1"
Modification Type: | Minor | Last Reviewed: | 8/16/2005 |
---|
Keywords: | KB114298 |
---|
|