R
Renate
Hi All,
I'm trying to complete a project for which it is needed to bind the
space key to a VBA procedure.
It seems that I cannot bind the macro to the space key in the template
manually so I binded the key in Document_New and Document_Open events.
No problem so far.
If I open an addition new document based on any other template then
mine the keybinding is not there and the space works as normal. Still
good.
If I activate the document based on my template however, the
keybinding does not exist anymore. Not good )-:
So I tried to find a workaround. I used a class module with the
WindowActivate event. In this event I check to see which document is
active (one based on my template or another) and bind the key
accordingly. However this seems not to be a stable workaround.
Sometimes it works, sometimes it doesn't.
I have a few questions.How do I bind a key specifically for documents
based on a template and not for other (active) documents? How can I
make sure the key is still binded even if the user browses through
several open documents which may or may not be documents based on the
template with my code?
Do I need to use another application event or am I completely in the
wrong way? How should I get this done?
This is the code I use to bind they key:
Sub BindSpaceKey()
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="Test",
KeyCode:=wdKeySpacebar
ActiveDocument.Saved = True
End Sub
(I need the ActiveDocument to be the CustomizationContext because the
real procedure Test changes a caption of the commandbar for the
specific document which can be different for multiple documents which
may be open based on my template)
The code in the WindowActivateEvent:
If RightDocument(Doc) Then
BindSpaceKey
End If
The function RightDocument checks to see if the document is based on
my template.
You can verify the keybindings strange behaviour if you create a new
template with the following code in the ThisDocument module:
Private Sub Document_New()
BindSpaceKey
End Sub
Private Sub Document_Open()
BindSpaceKey
End Sub
and in a normal module:
Sub BindSpaceKey(Optional iDummy As Integer)
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="Test",
KeyCode:=wdKeySpacebar
End Sub
Sub Test()
MsgBox "Test"
End Sub
I would expect that the spacebar shows the messagebox in documents
based on this template and not in other documents. If I have multiple
open documents and activate one not based on my template and return
however, the macro binded to the space key is gone. Why?
BTW This is Word 2003.
TIA for any help that one of you can give me!
I'm trying to complete a project for which it is needed to bind the
space key to a VBA procedure.
It seems that I cannot bind the macro to the space key in the template
manually so I binded the key in Document_New and Document_Open events.
No problem so far.
If I open an addition new document based on any other template then
mine the keybinding is not there and the space works as normal. Still
good.
If I activate the document based on my template however, the
keybinding does not exist anymore. Not good )-:
So I tried to find a workaround. I used a class module with the
WindowActivate event. In this event I check to see which document is
active (one based on my template or another) and bind the key
accordingly. However this seems not to be a stable workaround.
Sometimes it works, sometimes it doesn't.
I have a few questions.How do I bind a key specifically for documents
based on a template and not for other (active) documents? How can I
make sure the key is still binded even if the user browses through
several open documents which may or may not be documents based on the
template with my code?
Do I need to use another application event or am I completely in the
wrong way? How should I get this done?
This is the code I use to bind they key:
Sub BindSpaceKey()
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="Test",
KeyCode:=wdKeySpacebar
ActiveDocument.Saved = True
End Sub
(I need the ActiveDocument to be the CustomizationContext because the
real procedure Test changes a caption of the commandbar for the
specific document which can be different for multiple documents which
may be open based on my template)
The code in the WindowActivateEvent:
If RightDocument(Doc) Then
BindSpaceKey
End If
The function RightDocument checks to see if the document is based on
my template.
You can verify the keybindings strange behaviour if you create a new
template with the following code in the ThisDocument module:
Private Sub Document_New()
BindSpaceKey
End Sub
Private Sub Document_Open()
BindSpaceKey
End Sub
and in a normal module:
Sub BindSpaceKey(Optional iDummy As Integer)
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="Test",
KeyCode:=wdKeySpacebar
End Sub
Sub Test()
MsgBox "Test"
End Sub
I would expect that the spacebar shows the messagebox in documents
based on this template and not in other documents. If I have multiple
open documents and activate one not based on my template and return
however, the macro binded to the space key is gone. Why?
BTW This is Word 2003.
TIA for any help that one of you can give me!