tags:

views:

665

answers:

2

I have a project that adds functionality to Microsoft Word using an XML Expansion Pack. Currently, when the document we give the customer is opened, it loads our pack, which executes the SmartDocInitialize method which adds things to the main menu and toolbar using Microsoft.Office.Core.CommandBar.Controls.Add and the like. Without modifications, when opened in Word 2007 these buttons are added on the Add-Ins ribbon tab, but this isn't ideal since the buttons are all small, not grouped properly, and there is no way to bring the Add-Ins tab to the front when the document is loaded.

I would like to keep this functionality the same if the document is opened in Office 2003, but if the document is opened in Office 2007, I would like to read in an xml file which describes my new Ribbon tab and all of the buttons. Everything I've been able to find online has seemed to lead to the Ribbon file only being loaded if you have a very specific combination of magic (build in Visual Studio and it works, but no information on how you would deploy it to a users box) and will only work if you have an entire project created originally with the Visual Studio tools for Office properties, which I don't currently have.

Our development environment is XP, Visual Studio 2005, C#, .NET 2.0

+1  A: 

The Ribbon UI is just not as programatic as the old CommandBars UI. I don't think you can do what you want. As far as I know the only way to programatically modify the Ribbon UI is to have a (COM) Add-in implement the IRibbonExtensibility interface and return a custom XML file (with the Ribbon definition) from the GetCustomUI method. There is no way to add or remove buttons one at a time like you could with the CommandBars UI. It's just totally different. I don't believe you can do what you want from some macros in a document.

I'm not familiar with XML expansion packs, but if you have to install them on the user's PC, perhaps you could install an add-in as well that could load the appropriate ribbon XML for you.

Tom Winter
Everything I've seen for the COM Add-in's say that they are loaded with the application, not on a per-document basis. I can't handle it like this, unfortunately. I have figured out how to add the ribbon on a per-document basis, but with that way the onAction function seems to have to be a VBA macro, and I'm having a tough time figuring out how to get my VBA macro to call my event handlers in C#. If you know anything about that, please add an answer to this question: http://stackoverflow.com/questions/1288717/is-it-possible-to-call-c-dlls-from-visual-basic-code-contained-in-a-word-2007-fi
Ryan Ahearn
My guess is you'd have to make the .NET classes COM visible in some manner to VBA (which is COM bases) could see and work with them. That's outside of my expertise though.
Tom Winter
A: 

This isn't as hard as it seems.

If you already have your ribbon buttons hooked up to some VBA macros in the document, then you just need to add a reference in your VBA project to your com visible .net assembly.

The .net assembly in addition to being marked com visible (so it shows up as a type library in the references dialog box), has to have some methods that are setup to be callable from VBA.

google "vba .net callable"

its not difficult, mark class as com visible, declare it with a ProgID attribute, make methods public, make sure your methods return simple types, and use regasm to register the assembly on the target machine (not needed on dev box).

Anonymous Type