B
bmac
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
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