Word 2003 / VSTO / Customise Keyboard

R

Rob Stewart

Is it possible to assign shortcut keys in Word to code
that was developed in C# via Visual Studio Tools for
Office?

Going through Customise-->Keyboard allows shortcut keys to
be assigned to any VBA macro, but (understandably) C#
functions do not appear in the list.

I've tried assigning them in code via the C#
ThisDocument_Open() event using KeyBindings, but to no
avail (the code runs, but the keyboard remains
uncustomised).

Any pointers?

Thanks,
Rob
 
C

Cindy Meister -WordMVP-

Hi Rob,

I believe you need to either:
- place the VBA code in the VSTO document/template
- create the VBA code in the user's Normal.dot
- place the VBA code in a global addin template
- create a global COM Addin
Is it possible to assign shortcut keys in Word to code
that was developed in C# via Visual Studio Tools for
Office?

Going through Customise-->Keyboard allows shortcut keys to
be assigned to any VBA macro, but (understandably) C#
functions do not appear in the list.

I've tried assigning them in code via the C#
ThisDocument_Open() event using KeyBindings, but to no
avail (the code runs, but the keyboard remains
uncustomised).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan
24 2003)
http://www.mvps.org/word

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

Rob Stewart

Thanks Cindy, but I don't think that's quite what I meant.
I don't want to use VBA code for the shortcuts. I have
already converted the VBA to C#. How can I assign a
shortcut key to the C# code? The Word UI doesn't seem to
allow it - all it allows is assigning to VBA code, and I
haven't had any luck doing it programmatically using
KeyBindings (see original post).

Rob
 
R

Rob Stewart

Thanks Cindy, but I don't think that's quite what I meant.
I don't want to use VBA code for the shortcuts. I have
already converted the VBA to C#. How can I assign a
shortcut key to the C# code? The Word UI doesn't seem to
allow it - all it allows is assigning to VBA code, and I
haven't had any luck doing it programmatically using
KeyBindings (see original post).

Rob
 
C

Cindy Meister -WordMVP-

Hi Rob,

I knew what you meant, and what you want. But I don't think
you can do it (in this version of VSTO). I was listing what
is possible with keyboard shortcuts.

As I recall from discussions during VSTO beta, I think you
need something called a "callback", and if I understood
correctly, this means you have to have some VBA code in the
Word environment, somewhere. Here's an example (for Excel,
but same principle):
--
Here's an example for you that illustrates with the OnKey...

1. Create a new workbook and put the following code in a
module:

Dim managedObject As Object

Public Sub RegisterCallback(callback As Object)
Set managedObject = callback
Application.OnKey "^m", "DoManagedCallBack"
End Sub

Public Sub DoManagedCallBack()
managedObject.HandleCtrlM
End Sub

2. Then, create a new Excel project based on the existing
workbook you created.

3. Add the following code to the project:

Private Sub ThisWorkbook_Open() Handles ThisWorkbook.Open
ThisApplication.Run("RegisterCallback", Me)
End Sub

Public Sub HandleCtrlM()
MsgBox("HandleCtrlM")
End Sub

4. Press F5 to run; when you press Ctrl+m in Excel, the
DoManagedCallBack routine in the assembly is run.

So, what's happening here is that a) you call a macro in the
workbook to pass it a reference to the assembly's class, b)
the macro stores the reference to the object, c) you set up
OnKey to call a macro in the workbook, and d) when the
"OnKey" routine fires, it will use the stored reference to
the object to call a public method (HandleCtrlM in this
example) in the assembly code. Of course, for this to work,
the VBA security settings must allow the VBA code in the
workbook to run.
--

But you may want to ask for more details in the
vsnet.vstools.office newsgroup, where an occasional person
from MSFT drops by :)
Thanks Cindy, but I don't think that's quite what I meant.
I don't want to use VBA code for the shortcuts. I have
already converted the VBA to C#. How can I assign a
shortcut key to the C# code? The Word UI doesn't seem to
allow it - all it allows is assigning to VBA code, and I
haven't had any luck doing it programmatically using
KeyBindings (see original post).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

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