6

Is it possible to handle button click events on a custom ribbon button from another application using interop?

So if I've created a Word 2010 application add-in which creates a custom tab with several buttons and then I instantiate a Word application from a C# win forms application using interop, how would I then go about wiring up to the button click events on my custom ribbon from the win forms app? With Word 2003 I could access the buttons through the CommandBars collection and then simply wire up to the click event.

After some further reading, I guess what I'm trying to do is find out how to get access to the Ribbon object model using interop. Is this possible?

[Edit]

For anyone that's interested. When I wrote this question I knew of another way to solve my problem, but it means a lot of extra work changing my existing implementation to upgrade from Office 2003 to Office 2010, and so I was hoping there would be away to access the Ribbon object model outside of a VSTO add-in.

My solution is to use an IpcChannel to make calls from the vsto add-in to my win-forms application. So my win-forms application initialises the Word instance and then opens up a server channel. In the vsto add-in I handle Application.DocumentOpen and if the document is owned by my application I open a client channel to my application. I can then make calls back to my win-forms app from the vsto add-in through the use of shared interfaces.

While this technique works, it does have some draw backs. If the calls through the ipc channel are synchronous then my application can't touch the word object because a deadlock occurs. Making asynchronous calls introduces other problems, but I found that I could block the Word window with a modal dialog without ending up deadlocked.

While this isn't an exact answer to my original question, it is an alternative and so I thought I would share this with anyone else having this problem.

If anyone does know how to access the Ribbon object model outside of VSTO I would still be interested to know how.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Chris Lindsay
  • 121
  • 1
  • 6

1 Answers1

1

Edit: I finally discovered a code.msdn.microsoft.com project that incorporates native C# ribbon manipulation. Use that instead of any VBA business.

Original Post:

You don't need VSTO to access the ribbon programmatically. Visit Word Articles for a brief Word VBA example.

I'm certain there's a way to accomplish the same in C#, but I have yet to implement one. If I find one, I will be certain to share. (I previously contributed an answer that contains a C#-VBA workaround.)

Community
  • 1
  • 1
Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
  • Using the Accessibility interface looks interesting, but I don't want to use any VBA and there are several reasons for this. One is that the system I'm working on has literally millions of documents and hundreds of base documents and templates and so maintenance and version control of VBA code becomes a bit of an issue. None of our documents or templates are macro enabled and a high level macro security is enforced by our network admins. (Continued next comment) – Chris Lindsay Sep 15 '11 at 06:20
  • Using VBA does not solve the problem of communication with my winforms application, many years ago we used to use a VBA solution before moving to VSTO and we had to use SendMessage and PostMessage to notify the winforms app from VBA and override the WndProc method on the form, which wasn't a very pretty solution. So unless I can use the Accessibility interface to listen to mouse click events on custom ribbon controls directly from my C# winforms app, then my problem is not solved. – Chris Lindsay Sep 15 '11 at 06:21
  • However this is very close to answering my original question. – Chris Lindsay Sep 15 '11 at 06:24