Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Nov 2002
    Posts
    387
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to disable keys (2000/SR1)

    I need to know how to disable keys via VBA code in Word such as disabling teh key combination CTRL+C. In Excel VBA I am able to do it using .OnKey "^" but this fails in a module in a Word template. It seems the same code would work. Anyway how do I do it?

    Thanks

    Jim

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to disable keys (2000/SR1)

    <P ID="edit" class=small>(Edited by jscher2000 on 10-May-03 14:53. Long lines "continued" to fit the average browser window.)</P>OnKey is not valid in Word VBA.
    Excel and Word VBA do share many object model thingees, but mostly one needs investigate each object model separately.

    Word VBA does not actually have the equivalent of OnKey.
    OnKey is an Application level Method and affects the use of the keys in the entire Excel appp.

    In Word, keys are specified on a per template basis. So disabling a key Application wide is not really possible as sombody could open a doc attached to a different template and enable the key for that template.

    The following may be instructive. Run the TestDeleteKeyBinding Sub. Put the code in a template other than Normal.dot:
    <pre>
    Option Explicit
    Public Sub BagelsAndLox()
    MsgBox "Bagels and lox"
    End Sub

    Public Sub TestDeleteKeybinding()
    Dim keyKB As KeyBinding
    Dim keyKB1 As KeyBinding
    Dim strBuffer As String

    ' Clear all key bindings
    ' KeyBindings.ClearAll

    ' Assure that change affects only template containing macro, not Normal template
    CustomizationContext = MacroContainer
    ' Define key combination
    Set keyKB1 = FindKey(KeyCode:=BuildKeyCode(arg1:=wdKeyAlt, arg2:=wdKeyControl, _
    arg3:=wdKeyL))
    ' Add key binding to template
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="BagelsAndLox", _
    KeyCode:=BuildKeyCode(arg1:=wdKeyAlt, arg2:=wdKeyControl, arg3:=wdKeyL)
    strBuffer = ""
    For Each keyKB In KeysBoundTo(wdKeyCategoryMacro, "BagelsAndLox")
    strBuffer = strBuffer & keyKB.KeyString & " " & keyKB.Context.Name & _
    " " & keyKB.Command & vbCr
    Next
    ' List key bindings
    MsgBox strBuffer
    ' Disable key binding in template
    keyKB1.Rebind KeyCategory:=wdKeyCategoryDisable, Command:=""
    strBuffer = ""
    For Each keyKB In KeysBoundTo(wdKeyCategoryMacro, "BagelsAndLox")
    strBuffer = strBuffer & keyKB.KeyString & " " & keyKB.Context.Name & _
    " " & keyKB.Command & vbCr
    Next
    ' List key bindings
    MsgBox strBuffer
    ' KeyBindings.ClearAll
    End Sub
    </pre>


  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to disable keys (2000/SR1)

    You could also put a macro that shows a Messagebox ("Copy has been disabled. Blame Jim.") on Ctrl+C.

    You can reassign Ctrl+C to EditCopy if you decide you want it to work again, or just delete the macro.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •