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.
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.)
精彩评论