MS Office Forum / Word / Programming / November 2005
Coding the ENTER key to move between form fields in a protected fo
|
|
Thread rating:  |
bmac - 28 Oct 2005 18:22 GMT Good Afternoon All,
I'm new to VBA and I don't have a clue. I'm talking zero knowledge. I found a knowledge base article that does exactly what I need it to do. However, when the document containing the macros is opened and I open up a new Word document and I type some text in the document and hit the Enter Key, nothing happens. Is there some code in VBA that will apply the code to the active document and not to the normal.dot template. I have also double-checked to make sure that the code wasn't attached to the normal.dot template, but I guess somehow it has gotten attached. Any help will be greatly appreciated. I'm sending out an S.O.S.....I also apologize if there has already been a posting for question, but I couldn't seem to find it. I'm been working on this issue for 4 days now and I'm stumped. Here's my code:
Sub EnterKeyMacro() ' ' EnterKeyMacro Macro ' Macro created 10/14/2005 by BMcNearly
' 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 Else ' If the document is not protected for forms, ' insert a tab stop character. Selection.TypeText Chr(13) End If
End Sub Sub AutoNew() ' Do Not protect the document containing these macros. CustomizationContext = ActiveDocument.AttachedTemplate ' Bind the ENTER key to the EnterKeyMacro. KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _ KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro" ' Reprotect the document with Forms protection. ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End Sub Sub AutoOpen() ' This macro will reassign the ENTER key when you open an existing ' Word form fields document.
CustomizationContext = ActiveDocument.AttachedTemplate ' Bind the Enter key to the EnterKeyMacro. KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _ KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro" End Sub Sub AutoClose() CustomizationContext = ActiveDocument.AttachedTemplate FindKey(KeyCode:=BuildKeyCode(wdKeyReturn)).Disable ' Disables prompt to save template changes. Templates(1).Save End Sub
Thanks again...
bmac
Tony Jollans - 28 Oct 2005 20:53 GMT The code looks like it is designed to go in a template (.dot) rather than a document (.doc)
However, I'm not clear what you expect from it. If you
> open up a new Word document and I type some text in the document and hit the Enter Key,
then nothing will happen. It is designed to work with a document containing a Word Form (protected except for some designated input fields)
-- Enjoy, Tony
> Good Afternoon All, > [quoted text clipped - 90 lines] > > bmac bmac - 28 Oct 2005 21:40 GMT Thanks Tony for the response. I apologize if my question is out in left field. When a user opens the form (let's just call it TestForm) they can use the Enter Key or the Tab key to move from one field to the other. The code that I provided causes the Enter key to move from field to field in a protected document. The problem that I have is that if the user has to stop in the middle of filling out the form and wants to respond to an email (using Microsoft Word as the email editor) and they hit the Enter key, the Enter key will not move the cursor to the next line while the TestForm is opened. It is as if the macros are always running as long as the TestForm is opened. When the TestForm is closed, then the EnterKey works. Is there a way for me to modify the code below to only apply the macros to the ActiveDocument and not to the Normal.dot template? Any help will be greatly appreciated. I've read about putting borderless cells in the document and assigning exact dimensions to it, but doesn't seem to be playing nice either. Thanks.
bmac
> The code looks like it is designed to go in a template (.dot) rather than a > document (.doc) [quoted text clipped - 113 lines] > > > > bmac Doug Robbins - Word MVP - 29 Oct 2005 08:45 GMT If the reason for programming the Enter key to move from one field to the next was simply to stop the entry into a formfield from expanding and distorting the form, what was the problem that you encountered with setting the dimensions of table cells to exact dimensions which is the usual way of obtaining that effect.
However, an easy way (though untested) around your problem with the email situation maybe to use Shift+Enter
Or, do not use Word as the email editor.
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Thanks Tony for the response. I apologize if my question is out in left > field. When a user opens the form (let's just call it TestForm) they can [quoted text clipped - 146 lines] >> > >> > bmac Tony Jollans - 29 Oct 2005 09:58 GMT Hi bmac,
Ah, sorry, I misunderstood.
The critical factor here is where the customization, the setting of the Enter key, lives. Assuming the code is in your TestForm document the setting will be in the template attached to TestForm and, if that is normal.dot, the setting will apply everywhere. Having said that I have just done a very quick test and it does seem to behave slightly strangely when using Word as Outlook editor.
If the code lives in TestForm and applies only to TestForm, then it should not try to apply the setting to the Template - it is sufficient to apply it to the Document and, that way, it affects nothing else. To do that, change (in three places - AutoOpen, AutoNew, and AutoClose) ..
CustomizationContext = ActiveDocument.AttachedTemplate
to
CustomizationContext = ActiveDocument
If the code is in a Document (and not a Template) there is no need to keep the AutoNew - and even if it is in a Template, I suspect you don't really want it as it will make designing a new form difficult.
It would also help, generally, if the EnterKeyMacro included some code to deal with what the key did when not in a FormField - but that probably wouldn't matter if the key was only set in the protected document.
-- Enjoy, Tony
> Thanks Tony for the response. I apologize if my question is out in left > field. When a user opens the form (let's just call it TestForm) they can use [quoted text clipped - 130 lines] > > > > > > bmac bmac - 31 Oct 2005 02:40 GMT Hey Tony, I tried the following and still no joy. I made the procedures Private. This released the EnterKey when other documents are opened while the TestDocument is open, but now the form gets distorted. So, I'm back to square one. However, I did change the CustomizationContext = ActiveDocument. I feel that I'm really close, but there is a piece that is missing, and since I don't know what code to write on my own. I'm stumped. Any help will be greatly appreciated. Thanks for all your help so far. I think that I'm right on the verge of a breakthrough.
> Hi bmac, > [quoted text clipped - 182 lines] > > > > > > > > bmac Charles Kenyon - 31 Oct 2005 04:29 GMT The EnterKey procedure needs to be in either the document itself or in the document's template. Then it will not apply to other documents. You need only assign it to the Enter Key in the template or document once, making sure that your customization context is that template or document (and not normal.dot).
Was the knowledge base article you found: http://support.microsoft.com/default.aspx?scid=kb;en-us;211219 ?
If your users can't be trained to not use the Enter key, though, you might simply structure your forms so that the fields will not expand down. Then, if they press the Enter key, they can type all they want but it will not show up on your form or move other things on the form around.
 Signature Charles Kenyon
Word New User FAQ & Web Directory: http://addbalance.com/word
Intermediate User's Guide to Microsoft Word (supplemented version of Microsoft's Legal Users' Guide) http://addbalance.com/usersguide
See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! --------- --------- --------- --------- --------- --------- This message is posted to a newsgroup. Please post replies and questions to the newsgroup so that others can learn from my ignorance and your wisdom.
> Hey Tony, > I tried the following and still no joy. I made the procedures [quoted text clipped - 218 lines] >> > > > >> > > > bmac bmac - 31 Oct 2005 16:09 GMT Good Morning Charles, Yeah the article that you made reference to was the one that I used for my code. I have tried to make sure that the customization would not be applied to the Normal.dot template by using the suggestions that Tony gave me. My form is not set up such that I can use borderless table cells. I have tried Private and changing the customization code. I'm not sure what to do at this point. Any continued ideas would be greatly appreciated. Thanks.
bmace
> The EnterKey procedure needs to be in either the document itself or in the > document's template. Then it will not apply to other documents. You need [quoted text clipped - 231 lines] > >> > > > > >> > > > bmac Tony Jollans - 31 Oct 2005 18:56 GMT Hi bmac,
If you make the code private it won't run and so you won't have any settings. It must be public.
If you stick to the original code with the single change of making the keybinding in the activedocument you should be alright. You may find, however, that as a result of trying out different things you have a residual keybinding stored in normal which you may have to manually delete.
-- Enjoy, Tony
> Good Morning Charles, > Yeah the article that you made reference to was the one that I used [quoted text clipped - 194 lines] > > >> > > > > > >> > > > If ActiveDocument.FormFields(myformfield).Name <> _ ActiveDocument.FormFields(ActiveDocument.FormFields.Count) _
> > >> > > > .Name Then > > >> > > > ActiveDocument.FormFields(myformfield).Next.Select [quoted text clipped - 56 lines] > > >> > > > > > >> > > > bmac Charles Kenyon - 31 Oct 2005 19:14 GMT I just wrote the following code.
Sub EnterKeyMacro() MsgBox "This is the entrykey procedure" End Sub
Sub SetItNow() ' Do Not protect the template containing these macros. CustomizationContext = ThisDocument ' Bind the ENTER key to the EnterKeyMacro. KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _ KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro" End Sub
Sub ReSetIt() CustomizationContext = ThisDocument ' Bind the Enter key to the EnterKeyMacro. KeyBindings.ClearAll End Sub
The first one is the macro to substitute for the Enter key. It is simply a test and flashes a message box.
The second one sets the keybinding for the macro. It needs to be run only once and will be effective in that document from then on.
The third turns it off so you can actually do some work using the Enter key if you decide to revise your template.
The ThisDocument object refers to the document or template that contains the procedure being run.
Put the procedure you want for your enter key in your form's tempate. Then, in the template, run the second macro and it will set the key binding for that template and all documents based on the template. You only need to run this once.
The third macro is to reset keybindings (in that template only).
"Private" does not have anything to do with the customization context.
 Signature Charles Kenyon
Word New User FAQ & Web Directory: http://addbalance.com/word
Intermediate User's Guide to Microsoft Word (supplemented version of Microsoft's Legal Users' Guide) http://addbalance.com/usersguide
See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! --------- --------- --------- --------- --------- --------- This message is posted to a newsgroup. Please post replies and questions to the newsgroup so that others can learn from my ignorance and your wisdom.
> Good Morning Charles, > Yeah the article that you made reference to was the one that I [quoted text clipped - 280 lines] >> >> > > > >> >> > > > bmac bmac - 31 Oct 2005 22:43 GMT Thanks Charles and Tony, I will give your ideas a try and see how it goes. I will post a message later this evening to let you know the results. Thanks again.
bmac
> I just wrote the following code. > [quoted text clipped - 252 lines] > >> >> > > > > >> >> > > > myformfield = Selection.Bookmarks(1).Name bmac - 02 Nov 2005 21:12 GMT Charles and Tony, I'm still working on the code, but I do have a question for Charles. Do I add my original code for the EnterKey Macro and remove the MsgBox message so it will run and not just flash the message "This is the EnterKey Procedure? Any other ideas for me to try in case this doesn't work? So far, the code that Charles suggested is adding an additional line. So I don't know if the keybinding code is being called. Any help will be greatly appreciated. Attached is the code the way I have it now. Thanks a great deal guys.
Sub EnterKeyMacro()
MsgBox "This is the entrykey procedure" ' ' EnterKeyMacro Macro ' Macro created 10/14/2005 by BMcNearly
' 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 Else
' If the document is not protected for forms, ' insert a tab stop character.
Selection.TypeText Chr(13) End If
End Sub
Sub SetItNow() ' Do Not protect the template containing these macros. CustomizationContext = ThisDocument ' Bind the ENTER key to the EnterKeyMacro. KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), _ KeyCategory:=wdKeyCategoryMacro, Command:="EnterKeyMacro" End Sub
Sub ReSetIt() CustomizationContext = ThisDocument ' Bind the Enter key to the EnterKeyMacro. KeyBindings.ClearAll End Sub
> Thanks Charles and Tony, > I will give your ideas a try and see how it goes. I will post a [quoted text clipped - 250 lines] > > >> >> > > > > > >> >> > > > If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Charles Kenyon - 03 Nov 2005 03:58 GMT Yes. I didn't test your code in it. I was just checking to make sure that the code I wrote actually attached and detached from the Enter key when I ran the other macros. So, you put your code in the EnterKey macro.
An extra line as in inserts a paragraph mark?
 Signature Charles Kenyon
Word New User FAQ & Web Directory: http://addbalance.com/word
Intermediate User's Guide to Microsoft Word (supplemented version of Microsoft's Legal Users' Guide) http://addbalance.com/usersguide
See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome! --------- --------- --------- --------- --------- --------- This message is posted to a newsgroup. Please post replies and questions to the newsgroup so that others can learn from my ignorance and your wisdom.
> Charles and Tony, > I'm still working on the code, but I do have a question for [quoted text clipped - 378 lines] >> > >> >> > > > If ActiveDocument.ProtectionType = >> > >> >> > > > wdAllowOnlyFormFields bmac - 03 Nov 2005 15:58 GMT Good Morning Charles, Yes, when I said adding an additional line, I meant that it adds a paragraph mark. Sorry about that. I was saying that it wasn't moving to the next formfield when hitting the EnterKey, but it was adding a paragraph mark. Sorry for the confusion. Thanks
bmac
> Yes. I didn't test your code in it. I was just checking to make sure that > the code I wrote actually attached and detached from the Enter key when I [quoted text clipped - 237 lines] > >> > >> >> It would also help, generally, if the EnterKeyMacro included some > >> > >> >> code
|
|
|