Bind a macro the space key and keep it ??

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!
 
T

Tony Jollans

What you describe should work - and your demo works as expected for me. Does
it go wrong all the time, or just sometimes?
 
R

Renate

Hi Tony,
What you describe should work - and your demo works as expected for me. Does
it go wrong all the time, or just sometimes?

Thanks for your reply. This doesn't work at all for me and I've tried
on multiple systems. Does it work from you when you activate another
document that isn't based on the template and then move back to the
orginal one? In my case the keybinding is gone then.
 
T

Tony Jollans

Yes, it works for me. Just a quick test, of course, but it does work -
switching the keybinding on and off as I switch documents.

How are you activating your documents? Just by switching windows or is code
involved?

Incidentally, OnKey, as per Shane's post, is an Excel method.
 
R

Renate

Hi Tony,
Yes, it works for me. Just a quick test, of course, but it does work -
switching the keybinding on and off as I switch documents.

How are you activating your documents? Just by switching windows or is code
involved?

That's strange. It doesn't work for me (Word 2003 all patches included
and I can reproduce on multiple systems). There is no code involved in
activating another document. I just click another on on the taskbar.
Incidentally, OnKey, as per Shane's post, is an Excel method.
Thanks for clearing that up ;-)
 
T

Tony Jollans

I have played around with this a bit more and there is something odd
happening. I think you will need to rebind the key using an application
windowactivate event.
 
R

Renate

Hi Tiony,
I have played around with this a bit more and there is something odd
happening. I think you will need to rebind the key using an application
windowactivate event.

Sort of glad it is also happening to you ;-)
I thought about the WindowActivate event as well. I mentioned that in
my original post:

"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.
<>
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. "

This works sometimes, but is not stable at all. Sometimes I loose the
functionality of the space key at all in documents that are based on
my template but other times in plain blanc documents.

There must be somebody out here that got this to work?
 
T

Tony Jollans

I thought about the WindowActivate event as well. I mentioned that in
my original post:

So you did. When I found I had the problem I just looked for a way round -
which worked for me. I didn't go back and check what you had said earlier.
Sorry.

I can believe that it doesn't always work - I seem to recall reading
somewhere recently about some actions that disable application events but I
can't recall the details. I'm not aware of any other way (apart from using
Windows hooking) to make it always work. Word just doesn't seem to properly
keep track of this at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top