Keybindings in word addins

B

Birger Niss

Hi,

I'm trying to intercept Ctrl+S in a Word Addin. I have tried the following
code in Wd_DocumentChange:

Wd.KeyBindings.Add wdKeyCategoryCommand, "MyFunction",
BuildKeyCode(wdKeyControl, wdKeyS)

but I get the 5346 error "Word cannot change the function of the specified
key".

I suspect an error in the reference to MyFunction, because there is no
problem binding to the built-in functions like
FileOpen, FileClose etc.

Any help is greatly appreciated.


Birger Niss
 
C

Cindy M -WordMVP-

Hi Birger,
I'm trying to intercept Ctrl+S in a Word Addin. I have tried the following
code in Wd_DocumentChange:

Wd.KeyBindings.Add wdKeyCategoryCommand, "MyFunction",
BuildKeyCode(wdKeyControl, wdKeyS)

but I get the 5346 error "Word cannot change the function of the specified
key".

I suspect an error in the reference to MyFunction, because there is no
problem binding to the built-in functions like
FileOpen, FileClose etc.
Is "MyFunction" in a local VBA-project, or in your addin?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
B

Birger Niss

Hi Cindy,

It's in my addin

Best regards
Birger

Cindy M -WordMVP- said:
Hi Birger,

Is "MyFunction" in a local VBA-project, or in your addin?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy M -WordMVP-

Hi Birger,
It's in my addin
Then you'd need to have a callback function in a valid Word VBA project
(such as a global template). The keybinding would have to be assigned to
that.

There is NO WAY to link a keyboard shortcut with code in a COM Addin, only
with code IN a document.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
B

Birger Niss

Hi Cindy,

The strange thing is that - in an addin - you can easily intercept e.g.
clicks on the Save entry in the file menu or the diskette icon and associate
it with a function in your addin. Basically you subscribe to the events.
Ctrl+S should also fire an event that you could subscribe to in precisely
the same manner.

Thanks for taking time to help!

Best regards
Birger
 
B

Birger Niss

Hi Cindy,

I have now solved my basic problem simply by using the DocumentBeforeSaving
event in my addin.
I still feel though that it should be possible to subscribe to other
keyboard events.

Again thanks a lot for your input!

Best regards
Birger
 
C

Cindy M -WordMVP-

Hi Birger,
I still feel though that it should be possible to subscribe to other
keyboard events.
No arguments there, nothing of the sort has been enabled, yet, though.
Probably because key assigments have traditionally been reserved for
the user (and rightly so IMO). But there does need to be something
exposed for developers, now that MS are pushing the "code outside of
Office apps" more and more. But I suspect it will be a while, yet...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
W

wilsond

Cindy,

How do you capture those keyboard shortcuts in the code in a document? I
cannot allow CTRL-P, -S,-O, -K...

I tried using the "record macro" functionality in Word to get a VB code
example but it didn't code anything for when the CTRL-P was pressed...

David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?d2lsc29uZA==?=,
How do you capture those keyboard shortcuts in the code in a document? I
cannot allow CTRL-P, -S,-O, -K...

I tried using the "record macro" functionality in Word to get a VB code
example but it didn't code anything for when the CTRL-P was pressed...
You can't allow these, because of the commands they execute in Word? Or
because you want to use them for something yourself?

You can lock any individual key combination using the KeyBindings
collection/object (see the Word object model help). BE SURE to set the
appropriate CustomizationContext, otherwise you could end up ruining the
user's installation!

But note that this won't have the effect of disabling the commands
(FilePrint, FileSave, FileOpen...). If the user has also assigned any of
these to any other key combinations, those combinations will continue to
work.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
W

wilsond

Hi Cindy,


Cindy M -WordMVP- said:
Hi =?Utf-8?B?d2lsc29uZA==?=,

You can't allow these, because of the commands they execute in Word? Or
because you want to use them for something yourself?

It's because of the commands they execute by default in Word that I cannot
allow them.

You can lock any individual key combination using the KeyBindings
collection/object (see the Word object model help). BE SURE to set the
appropriate CustomizationContext, otherwise you could end up ruining the
user's installation!

But note that this won't have the effect of disabling the commands
(FilePrint, FileSave, FileOpen...). If the user has also assigned any of
these to any other key combinations, those combinations will continue to
work.

Hmm, thanks for that. I'll keep that in mind...

David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?d2lsc29uZA==?=,

Might be worthwhile, then, to loop the KeyBindings collection and check what
command each one executes. Then you can be relatively sure you're disabling
everything (that's currently assigned, anyway).
t's because of the commands they execute by default in Word that I cannot
allow them.


Hmm, thanks for that. I'll keep that in mind...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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