禁用键或组合键

时间:2015-05-08 06:39:14

标签: excel vba excel-vba office-2010

如何禁用Office 2010中的键组合或字符。

我尝试了以下代码,但它没有用。

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

1 个答案:

答案 0 :(得分:1)

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

以下参考链接

Excel Automation

SystemParametersInfo Function