Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro: Remapping Enter action to "Tab"? 1

Status
Not open for further replies.

AndrewJC

MIS
Sep 26, 2003
13
US
I'm working on creating a form in Word. We're trying to see if there's a way we can use Enter to navigate through the form, and although Microsoft has a way to do that in its Knowledge Base, it's not quite what we need, for this reason:

When you use the Microsoft script to navigate through a form using Enter, it causes the form to ignore any updates that the form fields have to make (i.e. if you have a text field set to capitalize everything, or if you have a specific date format in a date text field, they don't capitalize/change date format when you hit Enter).

My idea was that I simply use an AutoOpen macro to remap the Enter action as the Tab action, which will take care of that. Unfortunately, it seems that there is no easy way to simply use KeyBindings to map one key's function to mirror that of another key, only to run a function/macro/autotext entry.

Does anybody have any ideas on how I can do this?
 
You know, I thought about doing that. I'll have to give it a shot and see how it works.
 
Well, here's an interesting hiccup.

I tried doing what you suggested, and created a macro for the drop-down field to run on entry. The macro:
Code:
Sub ArrowKeysOn()
' This macro will reassign the arrow keys when you tab into a drop-down field.
    CustomizationContext = ActiveDocument
    ' Bind the arrow keys to the arrow key macros.
    'KeyBindings.Add KeyCode:=BuildKeyCode(38), _
        KeyCategory:=wdKeyCategoryMacro, Command:="UpArrow"
    KeyBindings.Add KeyCode:=BuildKeyCode(40), _
        KeyCategory:=wdKeyCategoryMacro, Command:="DownArrow"
End Sub
When I try to RUN said macro, however, it gives me the following error message:
Run-time error '5980':

The context cannot be modified.
The line it was erroring on was the KeyBindings command for the down arrow (I assume it would've done it on the up arrow macro if I'd had it uncommented at the time). So it appears that while there IS a way to identify the arrow keys using BuildKeyCode, it looks like you can't modify their default usage?
 
Haven't time to play with it at the moment but I guess it's to do with protection - you can't customize the document when it's protected (for forms, or other). Does it work if you do it to NormalTemplate (I know you don't want to do that really but it would prove a point)? Or if you unprotect and reprotect around it?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tried unprotecting and reprotecting around the script, and while it does seem to let me run the macro now... it's not DOING anything. The arrow keys simply act just like normal arrow keys, moving in and out of the form fields, instead of doing what I want them to do.

I'm starting to lose heart. :)
 
Well, well, well! You learn something every day.

It seems that Word overrides some keybindings in some circumstances. I'll have to investigate further to find the full extent of it.

I'm not sure what else to suggest. I have to say it doesn't seem unreasonable behaviour to want from a dropdown - it's how they sometimes behave in other situations, after all but I don't know how to achieve it here.

I'll post back if I think of anything, but don't hold your breath!

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Well, you have definitely been a great help to me, so thank you for your patience and advice!
 
AndrewJC

As it seems that your current method is giving a hard time, and as you don't know how to use forms on VBA I have prepared a small demo for you.

This is made of two templates. and can be downloaded from the following addresses (only for a few days!!!)


demo form.dot

countryform.dot holds the macros and user forms, and is the most important one. This is also the one where you would create more forms and macros.
Main point. Copy this to your word startup folder so it is loaded every time Word opens.

"my demo form.dot" this would be the same as your current template.
Main thing in terms of code is that you add a command to run a specific macro when you select a "new" document based on this template. (look at the code for more info).


How does it work.

On the template that the users see (My demo form.dot) you add the code to the "new" event, and you also need to add as many custom properties as you need.
On the case of this sample I added 3, "Name", "Address" and "Country".
These are the ones you will then use within the template to hold the text your users enter on the form.


On the macro template you add your forms, and when your users are happy and click a "accept" button, then you update the value of the custom properties defined on the other template (My demo form.dot on this case), you refresh the document and close the form. As this is VBA you can do whatever else you wish, including printing the document immediately.

If more explanations are required please ask.




Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top