Creating a Password-Style Macro Dialog Text Box

Last reviewed: August 4, 1997
Article ID: Q114299
6.00 6.00a 6.00c 7.00 WINDOWS kbusage kbmacro

The information in this article applies to:

  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, version 7.0

SUMMARY

In Word for Windows, there is no intrinsic setting for a password-style edit control. However, you can generate code to perform this function using the existing edit control. To create a dialog that prompts for a password, use SendMessage (Windows API function call) with the constant of EM_SETPASSWORDCHAR.

MORE INFORMATION

The following example demonstrates how to design a text box that will accept a user's password as input. It displays an asterisk (*) for each character entered.

Use the SendMessage API function call to create this dialog. 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 displayed character for the control. The argument EM_SETPASSWORDCHAR, as the second parameter to SendMessage, sets the desired text character to display based on the value specified by the third argument.

The SendMessage function requires the following parameters for setting the password Character

   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.

   wMsg    Specifies the window's message.

   wParam  Specifies the password character to display.

   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 SendMessageA Lib "User32"(hWnd As Long, wMsg As Integer,
wParam As Integer, lParam As String) As Long
Declare Function GetFocus Lib "User32"() As Long
Dim Shared WM_USER, PasChar, EM_SETPASSWORDCHAR
Sub MAIN
 WM_USER = 1024
 EM_SETPASSWORDCHAR = 204
 PasChar = Asc("*")
 Begin Dialog UserDialog 181, 78, "Password", .dlgfun
  OKButton 22, 6, 140, 21
  PushButton 22, 30, 140, 21, "Enter Password", .Push1
  TextBox 22, 54, 160, 18, .TextBox1
 End Dialog
 Dim dlg As UserDialog
 x = 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_SETPASSWORDCHAR, PasChar, "")
    dlgFun = 1
   End If
   If id$ = "OK" And Len(DlgText$("TextBox1")) > 0 Then
    MsgBox     "Your Password is:" + Chr$(10) + DlgText$("TextBox1")
   End If
  Case 3
  Case 4
  Case 5
  Case 6
  Case Else
 End Select
End Function

Word 6.0

Declare Function SendMessage Lib "User"(hWnd As Integer, wMsg As
Integer, wParam As Integer, lParam As String) As Long
Declare Function GetFocus Lib "User"() As Integer
Dim Shared WM_USER, PasChar, EM_SETPASSWORDCHAR
Sub MAIN
 WM_USER = 1024
 EM_SETPASSWORDCHAR = WM_USER + 28
 PasChar = Asc("*")
 Begin Dialog UserDialog 181, 78, "Password", .dlgfun
  OKButton 22, 6, 140, 21
  PushButton 22, 30, 140, 21, "Enter Password", .Push1
  TextBox 22, 54, 160, 18, .TextBox1
 End Dialog
 Dim dlg As UserDialog
 x = 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_SETPASSWORDCHAR, PasChar, "")
    dlgFun = 1
   End If
   If id$ = "OK" And Len(DlgText$("TextBox1")) > 0 Then
    MsgBox     "Your Password is:" + Chr$(10) + DlgText$("TextBox1")
   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"

Kbcategory: kbusage kbmacro KBSubcategory:


Additional reference words: 6.00 6.00a winword wordbasic word6 word7
6.00c 7.00 word95 masked api macro asterisk
Version : 6.00 6.00a 6.00c 7.00
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 4, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.