none
customize Microsoft Word 2007 in C#.net windows application using Microsoft.Office.Interop.Word RRS feed

  • Question

  • Hi.

    I am trying to disable some of the menu items of Microsoft word 2007 ribbon control. 

    Also trying to hide the word title bar.

    It, will be of great help to me , if anyone can give some light on the approach to follow for this.

    • Moved by edhickey Thursday, June 9, 2011 1:49 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Thursday, June 9, 2011 1:31 PM

Answers

  • Very thanks for your kind response. All I came to know through our conversations that, for word 2007 ribbon I need to use VSTO . But in my application I am doing so many functionalities with word 2003.                                                                                                                                                                 For example, again a sample code of my application below,  

    if (wd == null) wd = new Microsoft.Office.Interop.Word.Application();

    wd.Application.WindowBeforeRightClick +=

     In  lines  above, I am basically including a right click event . In this event I am doing some operations.                                                                              Now, if I use VSTO I will have to re-do all my existing operations all over agian And the task that I have done with word2003 needs to be re-worked.

    So, is there any alternative of using my existing and functionality in word2007 as well.

    new ApplicationEvents4_WindowBeforeRightClickEventHandler(Application_WindowBeforeRightClick);

    You do not need to use VSTO for Word 2007. As a matter of fact, you cannot use VSTO for a solution that's to be embedded in another application.

    Your basic code will work for Word 2007, but what will not work and cannot work are the things you want to do with the Ribbon. The Ribbon is different than command bars - this is explicitly by design. It is not possible for your application to directly manipulate the Ribbon - a Ribbon can only be affected in-process.

    You have no choice but to fundamentally re-design certain parts of your application if you need to support Office 2007 (or 2010). Those parts that need to be re-designed are anything that currently deal with Command Bars. There is NO alternative.


    Cindy Meister, VSTO/Word MVP
    Thursday, June 23, 2011 12:59 PM
    Moderator

All replies

  • I would suggest that you avoid Interop (COM) and build document customizations using native VSTO framework (Managed Code).


    Karl~
    Thursday, June 9, 2011 11:45 PM
  • Hi Biraj,

     

    Thanks for your post.

     

    What do you mean of “customize Microsoft Word 2007 in C#.net windows application using Microsoft.Office.Interop.Word

    Do you want to edit the ribbon of Office in WinForm application? Hmm…it is really not recommend (moreover I very suspect we can do this)

    The common way we change the ribbon of Office is using CustomUI editor (for document-level) and creating an add-in (for application level)

    For document-level:

    Change the Ribbon in Excel 2007 or Excel 2010

    Hide or Display Ribbon Tab/Group/Control with getVisible

     

    For application-level:

    Display and Hide Tabs, Groups, and Controls on the Microsoft Office Ribbon (Part 1 of 2)

    Display and Hide Tabs, Groups, and Controls on the Microsoft Office Ribbon (Part 2 of 2)

    Creating Custom Ribbon Galleries in Excel 2007

    Walkthrough: Creating a Custom Tab by Using the Ribbon Designer

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 1:01 PM
    Moderator
  • Hello Sir,

    Thanks for your response ..

    Actually,my requirement is that ... I want to load a word document in my WPF application where user can be able to update the document, like changing the Fonts, adding tables , color the text in the document. In other words , an user will be using the word menu to do this operations in my WPF application.

    But, I don't want the user to close the word instance from the word menu bar.

    Instead I will handle this in my application. Say, I will have a command button , on click of which  will close my application along with the word insatnce. Basically, I want to disable the save and File menu options for the word  editor and enable the other options ..

    I am able to achieve this with microsoft word 2003 .. but not in word 2007 since in 2007 menu bar has been replaced by ribbon control.

     

    Find a sample of my code ,

     

    private static Microsoft.Office.Interop.Word.Application wd = null;

     

    object fileName = @"C://abc.doc";  // location of word document file

    object newTemplate = false;

    object docType = 0;               

    object isVisible = true;               

           

     

    if(wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();        

    wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);

     

    //Sample code to enable/Disbale menu strip

    int counter = wd.ActiveWindow.Application.CommandBars.Count;

    for (int i = 1; i < counter; i++)

    {

        try

        {

            wd.ActiveWindow.Application.CommandBars[i].Enabled = true;                       

        }

        catch (Exception ex)

        {

     

        }

    }

     

    //Sample code to enable menu options like Save,Font...

    for (int intCnt = 0; intCnt < wd.ActiveWindow.Application.CommandBars[1].Controls.Count; intCnt++)

    {

        try

        {

            wd.ActiveWindow.Application.CommandBars[1].Controls[intCnt].Enabled = false;                       

        }

        catch { }

    }

     

    Regards,

    Biraj Kumar Dhekial 

     

    Thursday, June 16, 2011 5:08 AM
  • Hi Biraj 

     

    Thanks for your update.

     

    As we all know, menus and toolboxs in Office 2003 change to Ribbon through 2007. The CommandBar[x].Enable= false will not work for Office 2007 and above system. If you want to change ribbon, you have to use VSTO solution or CustomUI editor.

    Please see the link I posted in my last reply.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 16, 2011 9:59 AM
    Moderator
  • Hi Biraj

    You need to enable a Ribbon UI in your Word UI. I'm not familiar with how you'd include the Word.Application in a WPF project... Is this the Winword.exe installed locally on the user's machine? The key part for you is here, I think:

    if(wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();        

    wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);

     

     At this point, the document you're creating would need to provide the Ribbon UI, is that correct? In that case, your best bet is probably to create a custom Template and include the Ribbon XML in that. And any code required would have to be VBA code inside the template. I don't think you can use VSTO (so that the code for the Ribbon would be outside the template) because VSTO solutions aren't supported in an embedded environment (Word hosted in WPF).

     

     


    Cindy Meister, VSTO/Word MVP
    Thursday, June 16, 2011 12:49 PM
    Moderator
  • Hello sir,

    Very thanks for your kind response. All I came to know through our conversations that, for word 2007 ribbon I need to use VSTO . But in my application I am doing so many functionalities with word 2003.                                                                                                                                                                 For example, again a sample code of my application below,  

    if (wd == null) wd = new Microsoft.Office.Interop.Word.Application();

    wd.Application.WindowBeforeRightClick +=

    new ApplicationEvents4_WindowBeforeRightClickEventHandler(Application_WindowBeforeRightClick);

    In  lines  above, I am basically including a right click event . In this event I am doing some operations.                                                                              Now, if I use VSTO I will have to re-do all my existing operations all over agian And the task that I have done with word2003 needs to be re-worked.

    So, is there any alternative of using my existing and functionality in word2007 as well.

    Thanks,

    Thursday, June 23, 2011 11:16 AM
  • Very thanks for your kind response. All I came to know through our conversations that, for word 2007 ribbon I need to use VSTO . But in my application I am doing so many functionalities with word 2003.                                                                                                                                                                 For example, again a sample code of my application below,  

    if (wd == null) wd = new Microsoft.Office.Interop.Word.Application();

    wd.Application.WindowBeforeRightClick +=

     In  lines  above, I am basically including a right click event . In this event I am doing some operations.                                                                              Now, if I use VSTO I will have to re-do all my existing operations all over agian And the task that I have done with word2003 needs to be re-worked.

    So, is there any alternative of using my existing and functionality in word2007 as well.

    new ApplicationEvents4_WindowBeforeRightClickEventHandler(Application_WindowBeforeRightClick);

    You do not need to use VSTO for Word 2007. As a matter of fact, you cannot use VSTO for a solution that's to be embedded in another application.

    Your basic code will work for Word 2007, but what will not work and cannot work are the things you want to do with the Ribbon. The Ribbon is different than command bars - this is explicitly by design. It is not possible for your application to directly manipulate the Ribbon - a Ribbon can only be affected in-process.

    You have no choice but to fundamentally re-design certain parts of your application if you need to support Office 2007 (or 2010). Those parts that need to be re-designed are anything that currently deal with Command Bars. There is NO alternative.


    Cindy Meister, VSTO/Word MVP
    Thursday, June 23, 2011 12:59 PM
    Moderator