Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-05-2013, 03:39 PM
PosseJohn PosseJohn is offline Need Help With How To Use KeyBindings Windows XP Need Help With How To Use KeyBindings Office 2007
Novice
Need Help With How To Use KeyBindings
 
Join Date: Jul 2011
Posts: 20
PosseJohn is on a distinguished road
Default Need Help With How To Use KeyBindings

I have a document that has Fields for the user to edit. One of the things I've found useful is to capture when the user presses "Enter" and run some code:



On Document_Open I have the following:

Code:
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro"
The EnterKeyMacro is:

Code:
Sub EnterKeyMacro()
'Check whether the document is protected for forms and whether the protection is active.
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields And Selection.Sections(1).ProtectedForForms = True Then
        'Retrieve the bookmark of the current selection.  This is equivalent to the name of the form field.
        myformfield = Selection.Bookmarks(1).Name
        'Go to the next form field if the current form field is not the last one in the document.
        If ActiveDocument.FormFields(myformfield).Name <> ActiveDocument.FormFields(ActiveDocument.FormFields.Count).Name Then
            ActiveDocument.FormFields(myformfield).Next.Select
        Else
            'If the current form field is the last one, go to the first form field in the document.
            ActiveDocument.FormFields(1).Select
        End If
    End If
End Sub
This seems to work just fine in the document it was intended for, BUT if you have another Word document open, pressing ENTER results in no action (nothing happens).

How can I get the 'other' document to respond to the keypress of ENTER?

Thanks!
Reply With Quote
  #2  
Old 12-05-2013, 04:55 PM
fumei fumei is offline Need Help With How To Use KeyBindings Windows 7 64bit Need Help With How To Use KeyBindings Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Keybindings are document level functions. That is, they work in the document that has that keybinding. If the document does not have the keybinding...it does not have the keybinding.

That being said, if both documents are generated from the same template, and that template has the keybinding AND the keybinding is set with the Document_New event - when the template creates a new document - then the keybinding will work in both.
Code:
Private Sub Document_New()
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _
      KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro"
End Sub

I must say though that using the Enter key like this is very limited unless you also add code to make the Enter key acts like a normal Enter key. Otherwise it will NEVER act like a normal enter key.
Reply With Quote
  #3  
Old 12-05-2013, 05:03 PM
PosseJohn PosseJohn is offline Need Help With How To Use KeyBindings Windows 7 32bit Need Help With How To Use KeyBindings Office 2010 32bit
Novice
Need Help With How To Use KeyBindings
 
Join Date: Jul 2011
Posts: 20
PosseJohn is on a distinguished road
Default

I guess that's the part I'm missing, making the ENTER key behave as normal...

Ideas?

Thanx!
Reply With Quote
  #4  
Old 12-05-2013, 06:19 PM
macropod's Avatar
macropod macropod is offline Need Help With How To Use KeyBindings Windows 7 32bit Need Help With How To Use KeyBindings Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by fumei View Post
if both documents are generated from the same template, and that template has the keybinding AND the keybinding is set with the Document_New event - when the template creates a new document - then the keybinding will work in both.
...
I must say though that using the Enter key like this is very limited unless you also add code to make the Enter key acts like a normal Enter key. Otherwise it will NEVER act like a normal enter key.
Granted, but this is a long-established technique for making the Enter key behave in the same way as the Tab key when working with formfields. What's been posted so far should be part of a larger project. See: http://support.microsoft.com/kb/211219/en-au

Depending on what template is being used, the code may need to be coupled with an Application.DocumentChange Event macro that detects when the user switches documents and does some testing to determine whether to enable or disable the keybindings as per the code in the link.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-05-2013, 09:05 PM
fumei fumei is offline Need Help With How To Use KeyBindings Windows 7 64bit Need Help With How To Use KeyBindings Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Well, yes. That is why I posted
Quote:
unless you also add code to make the Enter key act like a normal Enter key
Like with an Else instruction.


PosseJohn, see the link macropod posted.
Reply With Quote
  #6  
Old 12-06-2013, 10:45 AM
PosseJohn PosseJohn is offline Need Help With How To Use KeyBindings Windows 7 32bit Need Help With How To Use KeyBindings Office 2010 32bit
Novice
Need Help With How To Use KeyBindings
 
Join Date: Jul 2011
Posts: 20
PosseJohn is on a distinguished road
Default Bindings Followup

So do I need to change the reference to the normal template?

I'm not sure how...
Attached Images
File Type: jpg ReferenceTemplate.JPG (30.7 KB, 20 views)
Reply With Quote
  #7  
Old 12-06-2013, 02:23 PM
macropod's Avatar
macropod macropod is offline Need Help With How To Use KeyBindings Windows 7 32bit Need Help With How To Use KeyBindings Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,369
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can't really do that if the Normal template is what's attached. You would need to use a different template, or add an Application.DocumentChange Event macro to the Normal template to handle the changes to/from your U1ChemistryShiftMeeting document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help With How To Use KeyBindings KeyBindings in PowerPoint? tinfanide PowerPoint 1 03-16-2013 10:13 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:39 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft