none
How to , in a Windows form application, modify the ribbon of a MS Word template

    Question

  • Hi,

    I have built a windows form appl. wich makes some modifications in context menus of word templates (adding menus to a "text" command bar  launching macros). The result is pretty good, but loading the modified template in MS Office is very very slow.

    Now, I would like to store these menus in a custom group of the ribbon of my Word Template. Is that possible in a windows form appl?  I know it is possible if using an Office template project but that is not a solution for me ? Wich references do I have to use?

    Thanx,

    Stephane

    Wednesday, December 12, 2012 9:32 AM

Answers

  • Hi Stephane

    If your template may not use VBA then what you want to do is not possible. A Ribbon defined as part of a document can only link to VBA code inside that document. If VBA is not acceptable, then you must have a VSTO document/template or, possibly, a VSTO add-in. Once the add-in is installed on the user's machine your Windows Form app can load/unload it as required.

    The Word APIs cannot create or edit a Ribbon. The Ribbon must be defined before the Word file - whether template or document - is loaded. If these menus are "static" (meaning at runtime you don't need to change the controls you show the user) then you can use the Custom UI Editor to define the Ribbon in the template.

    Note that if you use menus, there is a dynamic menu that - using the Ribbon VBA callbacks - can dynamically change the list of commands the user sees at any time during the session.

    If you need to decide what controls are to be used at runtime, then you can work with the Word template via the Open XML file format, before it's loaded into Word. You'd use the standard Packaging and XML namespaces to put the ribbon.xml and VBA project into the document, or modify the ribbon.xml already in the document.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Stef_09 Wednesday, December 12, 2012 3:40 PM
    Wednesday, December 12, 2012 1:53 PM
    Moderator
  • Hi Stephane

    <<So, I think there is no solution in visual studio.>>

    I'd say there's no solution at all, except what you're already doing. But you need to be aware that using CommandBars to change the user interface (context menus) is being phased out - eventually, that will not work anymore. Starting with Word 2010 the Ribbon is the primary tool to define customizations for the context menu.

    Closest you could get, I think, would be adding/modifying a Ribbon.xml and VBA project part to the document in order to accomodate the user being able to modify the menu. The VBA callbacks for the Ribbon could simply be "stubs" that call into a DLL that contains the actual code (and would be part of your windows Form solution).


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Stef_09 Wednesday, December 12, 2012 3:40 PM
    Wednesday, December 12, 2012 2:57 PM
    Moderator

All replies

  • Hi Stephane

    If your template may not use VBA then what you want to do is not possible. A Ribbon defined as part of a document can only link to VBA code inside that document. If VBA is not acceptable, then you must have a VSTO document/template or, possibly, a VSTO add-in. Once the add-in is installed on the user's machine your Windows Form app can load/unload it as required.

    The Word APIs cannot create or edit a Ribbon. The Ribbon must be defined before the Word file - whether template or document - is loaded. If these menus are "static" (meaning at runtime you don't need to change the controls you show the user) then you can use the Custom UI Editor to define the Ribbon in the template.

    Note that if you use menus, there is a dynamic menu that - using the Ribbon VBA callbacks - can dynamically change the list of commands the user sees at any time during the session.

    If you need to decide what controls are to be used at runtime, then you can work with the Word template via the Open XML file format, before it's loaded into Word. You'd use the standard Packaging and XML namespaces to put the ribbon.xml and VBA project into the document, or modify the ribbon.xml already in the document.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Stef_09 Wednesday, December 12, 2012 3:40 PM
    Wednesday, December 12, 2012 1:53 PM
    Moderator
  • Hi, Cindy, and many thanx for your comprehensive answer.

    VBA is not acceptable because my application must be able to use interfaces to access different kinds of documents' objects (toolbars in previous versions of MSWord and OpenOffice documents, context menus in MS Word21010 an maybe LibreOffice, ribbon menus in WORD2010...). The common point in all these implementations is a menu tree where the leafs are commands executed in the target document. There is a class implementing the interface for each document type.

    The tree is not modified at runtime when the document is loaded, but is only modified by my application.  But using the custom UI Editor is not an option because :

    1. it is the end user's responsability to adapt the tree. And the end user does not use the Custum UI editor;

    2. the same program should be used for the different types of documents (MSWord and other ...)

    And, due to the different document types (MSWord and other ...) I it will not be possible to use VSTO.

    So, I think there is no solution in visual studio.

    Best regards,

    Stephane

    Wednesday, December 12, 2012 2:51 PM
  • Hi Stephane

    <<So, I think there is no solution in visual studio.>>

    I'd say there's no solution at all, except what you're already doing. But you need to be aware that using CommandBars to change the user interface (context menus) is being phased out - eventually, that will not work anymore. Starting with Word 2010 the Ribbon is the primary tool to define customizations for the context menu.

    Closest you could get, I think, would be adding/modifying a Ribbon.xml and VBA project part to the document in order to accomodate the user being able to modify the menu. The VBA callbacks for the Ribbon could simply be "stubs" that call into a DLL that contains the actual code (and would be part of your windows Form solution).


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Stef_09 Wednesday, December 12, 2012 3:40 PM
    Wednesday, December 12, 2012 2:57 PM
    Moderator
  • <<The VBA callbacks for the Ribbon could simply be "stubs" that call into a DLL ...>>

    A little  tricky, but as you said, the only way ... I'll give it a try. And I hope the document with a customized ribbon will load faster than the one with  my comnandbar (there are about 200 to 300 leafs in my tree)

    Stephane

    • Marked as answer by Stef_09 Wednesday, December 12, 2012 3:38 PM
    • Unmarked as answer by Stef_09 Wednesday, December 12, 2012 3:40 PM
    Wednesday, December 12, 2012 3:23 PM
  • Hi Stephanie

    It should load faster. For one thing, the menus won't be created "on-the-fly" but will be in the document when it's opened. And the other side of the coin is that many things in the Ribbon are populated the first time the control is called, rather than when the document loads. So the users may experience a slight delay the first time they click on the top-level control for a menu tree.

    Yes, this approach would be a bit tricky, not least because you won't be able to manipulate the VBA project you insert into the Open XML file. VBA projects are still in the binary file format. So the VBA project has to be a finished module you simply plug-in.

    My recommendation would be to start work using the Custom UI Editor to create a basic Ribbon.XML with the things that all variations will require, even if that's only a Ribbon group on a built-in tab. Then add a test control that will use a callback in the VBA project. Make sure that's all working.

    Next step: create the COM-visible DLL with a method the VBA callback will call and register it under Windows. Create a Reference to the DLL in the VBA project and put in the code to call the method in the DLL. Test that.

    Once you have these basic structures working, the rest should be fairly straightforward.

    For manipulating the Ribbon.xml and importing the VBA project I recommend the Open XML SDK.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 4:05 PM
    Moderator
  • Once again, many thanx for your valuable help.

    I'll try Open XML SDK, though I did'nt use it before and don't know anything about it.

    Stephane

    Wednesday, December 12, 2012 4:38 PM
  • Hi Stephanie

    You don't need to use the SDK. If you're accustomed to working with Open XML already, using other tools, that's fine :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, December 12, 2012 4:53 PM
    Moderator