WD: Creating a Password-Style Macro Dialog Text Box

ID: Q114299


The information in this article applies to:


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 box that prompts for a password, use SendMessage (Windows API function call) with the constant of EM_SETPASSWORDCHAR.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
The following example demonstrates how to design a text box that accepts a user's password as input. It displays an asterisk (*) for each character entered.

Use the SendMessage API function call to create this dialog box. After you set the focus to the edit control you want, you must send a message to the window's message queue that resets the displayed character for the control. The argument EM_SETPASSWORDCHAR, as the second parameter to SendMessage, sets the text character you want 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

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"

Additional query words: 6.00a winword wordbasic word6 word7 6.00c word95 masked api macro asterisk


Keywords          : 
Version           : WINDOWS:6.0,6.0a,6.0c,7.0
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: July 14, 1999