|
Answer» Does anyone know how to write a macro for a Word template (a form) that would allow me to determine what happens when a particular key is pressed? I have checkboxes in the form but don't want the arrow keys or pageup/pagedown to SELECT the boxes. I can skip those form fields by using a tab order with macros but I cannot figure out how to give the pageup/pagedown and arrows keys a different function when pressed so they don't select the checkboxes in the form. I hope I made myself clear? thx!
code I'm using to determine tab order:
Sub ExitFieldName() ActiveDocument.Bookmarks("NextField").Range.Fields(1).Result.Select End Sub You can use the VB key constants to trigger a keypress event and then trap the keys.
vbKeyPageUp PAGE UP key vbKeyPageDown PAGE DOWN key vbKeyLeft LEFT ARROW key vbKeyUp UP ARROW key vbKeyRight RIGHT ARROW key vbKeyDown DOWN ARROW key
I hope this is what you wanted. That sounds like it could be what I need...but how do I implement it? Is there a SPECIAL way to add it to the general declaration with my other macros? sorry, I am a VB newbie Thanks for your response!When a key in a form is pressed, VB runs a keypress event. You will need to setup the event for each control (ie: Checkbox) on your form.
Example
Sub CheckBox1_KeyPress (KeyAscii As Integer) If KeyAscii = vbKeyPageUp Or _ KeyAscii = vbKeyPageDown Or _ KeyAscii = vbKeyLeft Or _ KeyAscii = vbKeyRight Or _ KeyAscii = vbKeyUp Or _ KeyAscii = vbKeyDown Then KeyAscii = 0 ' nullify the key action Beep End If End Sub
The above code CANCELS the action of various keys. Just cut and paste into your own keypress EVENTS. Naturally you can replace the logic with your own.
Just an idea: You can also setup a key mask to disable keys.
Hope this helps. Excellent, does it matter if the checkboxes are running an enter macro to make them exclusive, or if they are in a frame? My checkboxes are in a frame with an enter macro on each that will only allow you to select one (like a radio button). Unfortunately I'm not getting any reaction at all when I use this code in the document. :(
I tested the code against a regular text field in the form, and this doesn't seem to work either...no reaction at all when I press page up:
Sub TextField_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyPageUp Then MsgBox "hey" ' test key action Beep End If End Sub
Any ideas?Confusion reigns . I was able to get your macro to work, but I'm not sure if we're talking about the same situation. I created a form with a text box using the Word VB editor. I then coded a keypress event attached to the textbox. I also coded a AutoOpen macro (tools-->macros) to show the form when the document was loaded. When the textbox got focus, I pressed the pageup key and the message box appeared.
This is the technique I've always used in Excel. Apparently it works equally well in Word.
I also tried this with form fields, but couldn't figure out how to attach the macro to the control.
If you explain your design, I'll try to find a solution.
Bingo. That's exactly the problem I'm having. I am working with form fields in a Word doc ONLY...I have not created a User Form in VB. It's an employee review form, and each section has 5 checkboxes for ratings...if you tab through the document to fill in comments etc, PRESSING the up/down arrow keys or the pageup/pagedown keys will still move you through the checkboxes as well as the other form fields (regular text fields). This is a problem, as it selects each checkbox as you move through it. I can see someone mistakenly marking a checkbox this way, instead of specifically clicking on it with their mouse. What I'm trying to do is ignore the keypress event when on any of the checkbox form fields....but as you said, I can't figure out how to attach the code to them in the document. The code does not show up as an available enter/exit macro for the form field in the Word doc. If you find anything out on this please let me know, I really appreciate the help!I was unable to find out anything you don't already know. Unless you create a VB form, formfield macros can only be executed when the field gets or loses control; hardly what you need.
The link below may give you some ideas.
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=216
Good luck.
|