Masking Password in VBA Excel Input Box

冷暖自知 提交于 2021-01-29 05:20:24

问题


Could someone please help me to mask the password entered to the input box generated using the below code. I will be using Office 365 ProPlus.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim sPassCheck As String
    Dim rng As Range
    Dim sTemp As String
    Dim sPassword As String

    sPassword = "12345"
    sTemp = "You must enter the password to delete data"

    ' Check if target is within Range N6:N100000
    If Intersect(Target, Range("N6:N100000")) Is Nothing Then

        If Target.Count > 1 Then
            Set rng = Target.Cells(1, 1)
        Else
            Set rng = Target
        End If


        If rng.Value = "" Then

            sPassCheck = InputBox(sTemp, "Delete check!")

            Application.EnableEvents = False

            If sPassCheck <> sPassword Then Application.Undo

        End If
    End If

    Application.EnableEvents = True
End Sub

回答1:


Above link on comment should solve your problem. Here is like same codes. First copy and past below codes to a module

Option Explicit
Private Declare PtrSafe Function CallNextHookEx Lib "user32" (ByVal hHook As LongPtr, _
    ByVal ncode As LongPtr, ByVal wParam As LongPtr, lParam As Any) As LongPtr

Private Declare PtrSafe Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As LongPtr

Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
    (ByVal idHook As LongPtr, ByVal lpfn As LongPtr, ByVal hmod As LongPtr, ByVal dwThreadId As LongPtr) As LongPtr

Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As LongPtr

Private Declare PtrSafe Function SendDlgItemMessage Lib "user32" Alias "SendDlgItemMessageA" _
(ByVal hDlg As LongPtr, ByVal nIDDlgItem As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr

Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, _
ByVal lpClassName As String, ByVal nMaxCount As LongPtr) As LongPtr

Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As LongPtr

Private Const EM_SETPASSWORDCHAR = &HCC
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private Const HC_ACTION = 0

Private hHook As LongPtr


Public Function NewProc(ByVal lngCode As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
    Dim RetVal
    Dim strClassName As String, lngBuffer As LongPtr

    If lngCode < HC_ACTION Then
        NewProc = CallNextHookEx(hHook, lngCode, wParam, lParam)
        Exit Function
    End If

    strClassName = String$(256, " ")
    lngBuffer = 255

    If lngCode = HCBT_ACTIVATE Then
        RetVal = GetClassName(wParam, strClassName, lngBuffer)
        If Left$(strClassName, RetVal) = "#32770" Then
            SendDlgItemMessage wParam, &H1324, EM_SETPASSWORDCHAR, Asc("*"), &H0
        End If
    End If

    CallNextHookEx hHook, lngCode, wParam, lParam
End Function

Public Function PasswordBox(Prompt, Title) As String
    Dim lngModHwnd As LongPtr, lngThreadID As LongPtr

    lngThreadID = GetCurrentThreadId
    lngModHwnd = GetModuleHandle(vbNullString)

    hHook = SetWindowsHookEx(WH_CBT, AddressOf NewProc, lngModHwnd, lngThreadID)

    PasswordBox = InputBox(Prompt, Title)
    UnhookWindowsHookEx hHook
End Function

Then call PasswordBox() function from any where in workbook like.

Sub MaskedPassword()
    Range("A1") = PasswordBox("Enter your password.", "Paasword")
End Sub


来源:https://stackoverflow.com/questions/63006614/masking-password-in-vba-excel-input-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!