Paul_Hossler
05-09-2014, 07:59 AM
Is there an easy way to clear all .OnKey assignments without having to loop through a possilbe combinations of the shift keys, special keys, and letter keys?
This seems to work OK, but I don't like all the hard coding. So I was wondering if there was way to just release all at once
Option Explicit
Sub ClearAllOnKeys()
Dim iSpecial As Long, iShift As Long, iLetter As Long, iFunction As Long
Dim aSpecialKeys As Variant, aShiftKeys As Variant
aSpecialKeys = Array( _
"BS", "CAPSLOCK", "CLEAR", "DEL", "DOWN", "END", "ENTER", "~", "ESC", "HELP", "HOME", "INSERT", _
"LEFT", "NUMLOCK", "PGDN", "PGUP", "RETURN", "RIGHT", "SCROLLLOCK", "TAB", "UP")
aShiftKeys = Array(vbNullString, "+", "^", "%", "^%", "^+", "%+", "^%+")
For iShift = LBound(aShiftKeys) To UBound(aShiftKeys)
For iSpecial = LBound(aSpecialKeys) To UBound(aSpecialKeys)
Application.OnKey aShiftKeys(iShift) & "{" & aSpecialKeys(iSpecial) & "}"
Next iSpecial
For iFunction = 1 To 15
Application.OnKey aShiftKeys(iShift) & "{F" & iFunction & "}"
Next iFunction
For iLetter = Asc("a") To Asc("z") '97 to 122
Application.OnKey aShiftKeys(iShift) & Chr(iLetter)
Next iLetter
Next iShift
End Sub
This seems to work OK, but I don't like all the hard coding. So I was wondering if there was way to just release all at once
Option Explicit
Sub ClearAllOnKeys()
Dim iSpecial As Long, iShift As Long, iLetter As Long, iFunction As Long
Dim aSpecialKeys As Variant, aShiftKeys As Variant
aSpecialKeys = Array( _
"BS", "CAPSLOCK", "CLEAR", "DEL", "DOWN", "END", "ENTER", "~", "ESC", "HELP", "HOME", "INSERT", _
"LEFT", "NUMLOCK", "PGDN", "PGUP", "RETURN", "RIGHT", "SCROLLLOCK", "TAB", "UP")
aShiftKeys = Array(vbNullString, "+", "^", "%", "^%", "^+", "%+", "^%+")
For iShift = LBound(aShiftKeys) To UBound(aShiftKeys)
For iSpecial = LBound(aSpecialKeys) To UBound(aSpecialKeys)
Application.OnKey aShiftKeys(iShift) & "{" & aSpecialKeys(iSpecial) & "}"
Next iSpecial
For iFunction = 1 To 15
Application.OnKey aShiftKeys(iShift) & "{F" & iFunction & "}"
Next iFunction
For iLetter = Asc("a") To Asc("z") '97 to 122
Application.OnKey aShiftKeys(iShift) & Chr(iLetter)
Next iLetter
Next iShift
End Sub