Search code examples
vbaexceloffice-2010

Disable key or key combination


how do I disable Key Combination or characters office 2010.

I tried the following code but it did not work.

Sub Disable_Keys()
    Dim KeysArray As Variant
    Dim Key As Variant

    KeysArray = Array("@", "!", "~", "%", "SHIFT+F12")

    'Disable the StartKeyCombination key(s) with every key in the KeysArray
    For Each Key In KeysArray
        Application.OnKey Key, "myMsg"
    Next Key
End Sub

Solution

  • Private Sub Workbook_Open()
      '// Disable Accelerators keys in Excel 2007-2013
        Dim Ctl As CommandBarControl
            For Each Ctl In Application.CommandBars("&Legacy Keyboard Support").Controls
                Ctl.Tag = Ctl.Caption
                Ctl.Caption = Replace(Ctl.Caption, "&", "")
            Next
        Dim StartKeyCombination As Variant
        Dim KeysArray As Variant
        Dim Key As Variant
        Dim I As Long
        On Error Resume Next
            '// Shift key = "+"  (plus sign)
            '// Ctrl key = "^"   (caret)
            '// Alt key = "%"    (percent sign
            '// We fill the array with this keys and the key combinations
            '// Shift-Ctrl, Shift- Alt, Ctrl-Alt, Shift-Ctrl-Alt
            For Each StartKeyCombination In Array("+", "^", "%", "+^", "+%", "^%", "+^%")
                KeysArray = Array("{BS}", "{BREAK}", "{CAPSLOCK}", "{CLEAR}", "{DEL}", _
                                  "{DOWN}", "{END}", "{ENTER}", "~", "{ESC}", "{HELP}", "{HOME}", _
                                  "{INSERT}", "{LEFT}", "{NUMLOCK}", "{PGDN}", "{PGUP}", _
                                  "{RETURN}", "{RIGHT}", "{SCROLLLOCK}", "{TAB}", "{UP}")
    
            '// Disable the StartKeyCombination
             For Each Key In KeysArray
                 Application.OnKey StartKeyCombination & Key, ""
             Next Key
             '// Disable the StartKeyCombination key(s) with every other key
             For I = 0 To 255
                 Application.OnKey StartKeyCombination & Chr$(I), ""
             Next I
             '// Disable the F1 - F15 keys in combination with the Shift, Ctrl or Alt key
             For I = 1 To 15
                 Application.OnKey StartKeyCombination & "{F" & I & "}", ""
             Next I
         Next StartKeyCombination
         '// Disable the F1 - F15 keys
         For I = 1 To 15
             Application.OnKey "{F" & I & "}", ""
         Next I
         '// Disable the PGDN and PGUP keys
         Application.OnKey "{PGDN}", ""
         Application.OnKey "{PGUP}", ""
     End Sub
    

    Reference below links

    Excel Automation

    SystemParametersInfo Function