none
How to collapse ribbon for office 2016 Word 2016 and Excel 2016 using c# code or VSTO. RRS feed

  • Question

  • Hi ,

     How to collapse ribbon for office 2016 Word 2016 and Excel 2016 using c# code or VSTO.

    Please give any suggestions.

    Regards,

    Kiran R.

    Friday, March 16, 2018 9:02 AM

Answers

  • But is your program actually executing the code that corresponds to the .height = 0 value?

    And what happens when you step through that HideRibbon line? Can you verify that trying to execute another MsoId such as "About" instead of "HideRibbon"

    I am really trying to establish whether or not the code is executing the path I expect, and whether or not the .ExecuteMso is doing anything at all.

    This is the full code of my test console app - you need to change the document path+name to be a .docx that exists on your system, then close Word before stepping through the code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordapp = new Word.Application();
                wordapp.Visible = true;
                Word.Document MyWordDocument = wordapp.Documents.Add("d:\\test\\2018031301 numbering in ooxml\\doc1.docx");
                MyWordDocument.Activate();
                Microsoft.Office.Core.CommandBar cmdbarRibbon = wordapp.CommandBars["Ribbon"];
                int theHeight = cmdbarRibbon.Height;
                wordapp.CommandBars.ExecuteMso("HideRibbon");
                if (cmdbarRibbon.Height > theHeight) // It was hidden so try minimize
                {
                    theHeight = cmdbarRibbon.Height;
                 
       wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                else // it is now hidden, so revert then minimize
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");                wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                if (cmdbarRibbon.Height > theHeight) // It was already minimized so revert
                {              
                 
       wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
    
                //MyWordDocument.Close();
                //wordapp.Quit();
            }
        }
    }
    



    Peter Jamieson

    • Marked as answer by Kiran Reddi Tuesday, March 27, 2018 9:37 AM
    Monday, March 26, 2018 6:39 PM
  • As far as I can tell, the ribbon display can only be in three states:

    The HideRibbon and MinimizeRibbon are both unpressed

    HideRibbon is unpressed and MinimizeRibbon is pressed

    HideRibbon is pressed and MinimizeRibbon is unpressed.

    I realised that you do not need to mess around checking heights, so for that you might want to look at the code I posted later.

    If you want to do other things dynamically with the ribbon, I think you may need to use VSTO and its Globals.Ribbons classes. For that, you would probably be better off looking/asking in the VSTO group here at https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=vsto

    (there is an introduction at https://msdn.microsoft.com/en-us/library/bb772088.aspx)

    some revised test code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
     
            {
                Word.Application wordapp = new Word.Application();
                wordapp.Visible = true;
                Word.Document MyWordDocument = wordapp.Documents.Add("d:\\test\\2018031301 numbering in ooxml\\doc1.docx");
                MyWordDocument.Activate();
    
                Microsoft.Office.Core.CommandBar cmdbarRibbon = wordapp.CommandBars["Ribbon"];
    
                // report initial state
                System.Console.WriteLine("Initial state");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                // ensure Hide and Minimize are both unpressed
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                }
                if (wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                System.Console.WriteLine("HideRibbon and MinimizeRibbon should be unpressed");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                // ensure Hide is unpressed and Minimize is pressed
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                }
                if (!wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                System.Console.WriteLine("HideRibbon should be unpressed and MinimizeRibbon should be pressed");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
     
                // ensure Hide is pressed and Minimize is unpressed
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    if (wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        // unhide, unminimize, rehide
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                    }
                }
                else
                {
                    if (wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                    }
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                }
                System.Console.WriteLine("HideRibbon should be pressed and MinimizeRibbon should be unpressed");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                // try to ensure Hide is pressed and Minimize is pressed
                // you may need to uncomment the following line to check what happens when
                // we start this part when HideRibbon is unpressed
                //wordapp.CommandBars.ExecuteMso("HideRibbon");
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    System.Console.WriteLine("1-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
    
                    if (!wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        // unhide, minimize, rehide
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                        System.Console.WriteLine("2-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                        System.Console.WriteLine("3-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                    }
                }
                else
                {
                    System.Console.WriteLine("4-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                    if (!wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                        System.Console.WriteLine("5-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                    }
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                    System.Console.WriteLine("6-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                }
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                //MyWordDocument.Close();
                //wordapp.Quit();
            }
        }
    }
    


    Peter Jamieson

    • Marked as answer by Kiran Reddi Wednesday, April 4, 2018 2:12 PM
    Wednesday, April 4, 2018 9:22 AM
  • It would be Documents.Open (not Document.Open) but generally speaking, messages to do with protected mode are there to protect the user from malware. It is not always possible for code to prevent such messages appearing. If the user wishes to prevent them, they typically have to modify the security options in Word, or set the relevant documents/folders as "trusted".

    If the problem is arising because the document is set to read-only in the file system, in some cases you may be able to change the relevant setting in the file system before opening the file, using e.g.

    File.SetAttributes(fileName, FileAttributes.Normal);

    There may have been a problem in a recent "Insider" build of Word - see e.g. https://stackoverflow.com/questions/48626735/protocol-handler-broken-for-protected-mode-in-office-insider-fast-build


    Peter Jamieson

    • Marked as answer by Kiran Reddi Thursday, June 21, 2018 8:42 AM
    Wednesday, June 20, 2018 5:31 PM

All replies

  • How about

    yourdocumentobject.ActiveWindow.ToggleRibbon();


    Peter Jamieson

    Saturday, March 17, 2018 1:27 PM
  • Except that isn't going to be enough. I've also edited this as exception handling can also be avoided.

    Detecting the current state is the problem. The followinng assumes that what you want to end up with is the ribbon  showing tabs only, no matter which state it started in. I think it would be fairly easy to adapt to cope with other requirements.

    There seem to be three possible states (hidden, or auto-hide, "tabs only", and the full thing. The idMso to hide the Ribbon (HideRibbon) and the idMso to minimize it (MinimizeRibbon) are both toggles, but if the ribbon is hidden, minimisation throws an exception.

    So to avoid that, use the Hide option first - it should never throw an exception. Then test the Height to see whether the window is now hidden or not hidden. 

    You will probably need to pay more attention to changes to the display state (maximised/not-maximised) of the window as a whole, as that may change as a side effect of modifying the ribbon's display state.

                Microsoft.Office.Core.CommandBar cmdbarRibbon = wordapp.CommandBars["Ribbon"];
                int theHeight = cmdbarRibbon.Height;
                wordapp.CommandBars.ExecuteMso("HideRibbon");
                if (cmdbarRibbon.Height > theHeight) // It was hidden so try minimize
                {
                    theHeight = cmdbarRibbon.Height;
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                else // it is now hidden, so revert then minimize
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                if (cmdbarRibbon.Height > theHeight) // It was already minimized so revert
                {
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }


    Saturday, March 17, 2018 4:46 PM
  • Hello Kiran Reddi,

    What's the current state of the thread? Could Peter's suggestion work for you?

    If it does, please mark helpful reply to close this thread.

    If not, please feel free to follow up to let us know the current issue.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 22, 2018 5:53 AM
  • Hi Peter/Terry,

        I just have tested the above block code , even the ribbon is maximized/minimized , it showing the height =0 only,

    After that it is not effecting anything in collapsing ribbon in Ms Word in application.

    My Project Details ::

    Dlls : 

          1. Interop.Word - Version= 8.2.0.0

          2. Interop.Excel - Version= 1.4.0.0

    .Net Framework version = 2.0

    Could you give any suggestions..


    R L Kiran


    • Edited by Kiran Reddi Monday, March 26, 2018 12:37 PM
    Monday, March 26, 2018 12:32 PM
  • So you are setting the wordapp to a suitable Word.Application object, and when you step through the code, it should be executing this part:

     else // it is now hidden, so revert then minimize
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");

    Is that correct?

    But you do not see the ribbon changing after that is executed?


    Peter Jamieson

    Monday, March 26, 2018 12:38 PM
  • Re. the version numbers, as far as I know Interop.Word v 8.1.0 is the version appropriate to Word 2000, which did not even have the Ribbon, so I do not know what would happen as a consequence of trying to execute my code.

    Here, I am using Microsoft.Office.Interop.Word v 15.0.0.0 and .Net Framework 4.6.1


    Peter Jamieson

    Monday, March 26, 2018 2:18 PM
  • Hi Peter,

        Here I updated to latest version the dll's  - Microsoft.Office.Interop.Word v 15.0.0.0 and updated .Net Framework as 4.6.1.

    Just have tried , still didn't identify the height of ribbon is zero value even it is minimized/ maximized.


    R L Kiran

    Monday, March 26, 2018 5:27 PM
  • But is your program actually executing the code that corresponds to the .height = 0 value?

    And what happens when you step through that HideRibbon line? Can you verify that trying to execute another MsoId such as "About" instead of "HideRibbon"

    I am really trying to establish whether or not the code is executing the path I expect, and whether or not the .ExecuteMso is doing anything at all.

    This is the full code of my test console app - you need to change the document path+name to be a .docx that exists on your system, then close Word before stepping through the code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordapp = new Word.Application();
                wordapp.Visible = true;
                Word.Document MyWordDocument = wordapp.Documents.Add("d:\\test\\2018031301 numbering in ooxml\\doc1.docx");
                MyWordDocument.Activate();
                Microsoft.Office.Core.CommandBar cmdbarRibbon = wordapp.CommandBars["Ribbon"];
                int theHeight = cmdbarRibbon.Height;
                wordapp.CommandBars.ExecuteMso("HideRibbon");
                if (cmdbarRibbon.Height > theHeight) // It was hidden so try minimize
                {
                    theHeight = cmdbarRibbon.Height;
                 
       wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                else // it is now hidden, so revert then minimize
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");                wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                if (cmdbarRibbon.Height > theHeight) // It was already minimized so revert
                {              
                 
       wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
    
                //MyWordDocument.Close();
                //wordapp.Quit();
            }
        }
    }
    



    Peter Jamieson

    • Marked as answer by Kiran Reddi Tuesday, March 27, 2018 9:37 AM
    Monday, March 26, 2018 6:39 PM
  • Hi Peter,

      Here for AUTO-Hide ribbon is same as well , or any difference with hide/minimize ribbon.Any suggestions..


    R L Kiran

    Wednesday, March 28, 2018 3:50 PM
  • 1. I am not sure I understood that. What is the same?

    2. Did you try the code I posted, but with a .docx of your own? When you marked it as the answer, I assumed you had, and had been able to work out what was going wrong before.

    3. Can you open Word and VBA, and execute the equivalent commands in the Immediate window? e.g.

    Commandbars.ExecuteMso("HideRibbon")

    and see what happens?


    Peter Jamieson

    Wednesday, March 28, 2018 4:43 PM
  • Hi Peter Jamieson,

          As per your conversation,

    1. I am not sure I understood that. What is the same?

    Here I am looking for the collapsing ribbon types.

    A).We have identified for Hide Commands by Collapsing Ribbon as follows.

    B).  Here looking for the other type of-collapse ribbon like  Auto-Hide Ribbon as follow screenshot.

    This is what I am saying what you provided above code (Show Tabs but collapse commands) is same for Auto-Hide Ribbon scenario code as well.

    I had tried your code not coming as Autohide ribbon in word as below. 

    ii. AutoHide Ribbon

    Without Tabs and commands also as like below scrrenshot 

    2. Did you try the code I posted, but with a .docx of your own? When you marked it as the answer, I assumed you had, and had been able to work out what was going wrong before.

    Yes , I have tried with .docx and its working fine after doing some modification in my code.

    Before error came in my application but working fine in my POC as given by u ,the line is : 

    Word.Document MyWordDocument = wordapp.Documents.Add(@"xxxxx\testing\WordDocFiles\OneParagraphFiveWordsMismatchNegtive.docx");

    Now changed and working fine in my application flow as expected by adding like this :::   wordapp.Documents.Add(ref filename,ref missing,ref missing,true);

    3. Can you open Word and VBA, and execute the equivalent commands in the Immediate window? e.g.

    Commandbars.ExecuteMso("HideRibbon")

    and see what happens?

    => I tried  , but didn't get exact steps to check the scenarios as given by you.Could you give example how check commands in immediate window, as i am new for this.

       => Could you give suggestion for Auto-Hide Ribbon using C# in Word 2016 as per below screenshot.

    ii. AutoHide Ribbon

    Without Tabs and commands also as like below scrrenshot 

    Eagerly waiting for your reply,

    Regards,

    Kiran R


    R L Kiran


    Tuesday, April 3, 2018 6:43 PM
  • OK,

    1. As far as I am aware, when you Hide the Ribbon it is the same as using the AutoHide option. If you want to remove the ribbon, that is a different thing.

    2. re. checking things in the Immediate Window, what I mean is this:

     a. open Word. Probably better to open a new blank document too.

     b. click Alt-F11 to open the Visual Basic Editor (VBE). (If it does not open, you may need to enable the Developer tab, e.g. go to File->Options->Customize Ribbon, and check the box next to "Developer" in the second column. Then open the developer tab in the ribbon, and click the Visual Basic icon (in the English language version of Word, it's at the left hand end of the tab).

     c. In the VBE, use ctrl-G or View->Immediate Window to display the Immediate Window. In there you can type a piece of VBA, press enter, and it should execute. So if you arrange the VBE and Word on your screen, you should see the ribbon hiding and re-displaying when you type the command I suggested. Also, you can inspect value by putting a "?" character at the beginning of the line (it's basically shorthand for the VBA command "Print"). So for example, you can chek the name of the document using

    ?Documents(1).Name

    Using VBA to check pieces of code can be very useful because most things you can do to the Word Object Model should work the same way in a correctly written C# program (although the C# syntax may be different, and you might have to do some things differently, e.g. create new application instances and so on). So if it works in VBA but not in C# it can be a sign that something is not right in the c# program, or that the C# program is actually working with a different object (for example, an invisible copy of Word) than the one you are actually looking at when you test the code.


    Peter Jamieson

    Tuesday, April 3, 2018 7:27 PM
  • HI Peter,

    Thanks for quick reply and brief explanation step by step.

       Could you give idea of removing ribbon in Word 2016 using C# as mentioned above AutoHide Ribbon (without Tabs and commands). 

      


    R L Kiran



    • Edited by Kiran Reddi Wednesday, April 4, 2018 2:33 AM
    Wednesday, April 4, 2018 2:32 AM
  • As far as I can tell, the ribbon display can only be in three states:

    The HideRibbon and MinimizeRibbon are both unpressed

    HideRibbon is unpressed and MinimizeRibbon is pressed

    HideRibbon is pressed and MinimizeRibbon is unpressed.

    I realised that you do not need to mess around checking heights, so for that you might want to look at the code I posted later.

    If you want to do other things dynamically with the ribbon, I think you may need to use VSTO and its Globals.Ribbons classes. For that, you would probably be better off looking/asking in the VSTO group here at https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=vsto

    (there is an introduction at https://msdn.microsoft.com/en-us/library/bb772088.aspx)

    some revised test code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
     
            {
                Word.Application wordapp = new Word.Application();
                wordapp.Visible = true;
                Word.Document MyWordDocument = wordapp.Documents.Add("d:\\test\\2018031301 numbering in ooxml\\doc1.docx");
                MyWordDocument.Activate();
    
                Microsoft.Office.Core.CommandBar cmdbarRibbon = wordapp.CommandBars["Ribbon"];
    
                // report initial state
                System.Console.WriteLine("Initial state");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                // ensure Hide and Minimize are both unpressed
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                }
                if (wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                System.Console.WriteLine("HideRibbon and MinimizeRibbon should be unpressed");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                // ensure Hide is unpressed and Minimize is pressed
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                }
                if (!wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                {
                    wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                }
                System.Console.WriteLine("HideRibbon should be unpressed and MinimizeRibbon should be pressed");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
     
                // ensure Hide is pressed and Minimize is unpressed
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    if (wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        // unhide, unminimize, rehide
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                    }
                }
                else
                {
                    if (wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                    }
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                }
                System.Console.WriteLine("HideRibbon should be pressed and MinimizeRibbon should be unpressed");
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                // try to ensure Hide is pressed and Minimize is pressed
                // you may need to uncomment the following line to check what happens when
                // we start this part when HideRibbon is unpressed
                //wordapp.CommandBars.ExecuteMso("HideRibbon");
                if (wordapp.CommandBars.GetPressedMso("HideRibbon"))
                {
                    System.Console.WriteLine("1-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
    
                    if (!wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        // unhide, minimize, rehide
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                        System.Console.WriteLine("2-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                        wordapp.CommandBars.ExecuteMso("HideRibbon");
                        System.Console.WriteLine("3-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                    }
                }
                else
                {
                    System.Console.WriteLine("4-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                    if (!wordapp.CommandBars.GetPressedMso("MinimizeRibbon"))
                    {
                        wordapp.CommandBars.ExecuteMso("MinimizeRibbon");
                        System.Console.WriteLine("5-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                    }
                    wordapp.CommandBars.ExecuteMso("HideRibbon");
                    System.Console.WriteLine("6-MinimizeRibbon is now " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                }
                System.Console.WriteLine("HideRibbon is " + wordapp.CommandBars.GetPressedMso("HideRibbon").ToString());
                System.Console.WriteLine("MinimizeRibbon is " + wordapp.CommandBars.GetPressedMso("MinimizeRibbon").ToString());
                System.Console.WriteLine("Press the spacebar");
                System.Console.ReadKey();
    
                //MyWordDocument.Close();
                //wordapp.Quit();
            }
        }
    }
    


    Peter Jamieson

    • Marked as answer by Kiran Reddi Wednesday, April 4, 2018 2:12 PM
    Wednesday, April 4, 2018 9:22 AM
  • Thanks a lot Peter Jameison.

    It covers all the scenarios of collapse ribbon types in word, this is best article and explanation .

    Great effort ... good job Peter .


    R L Kiran

    Wednesday, April 4, 2018 2:11 PM
  • Hi Peter,

          Is it mandatory to use document.Add() instead can we use any other method or property.

    Beacause I am facing issue while loading documents  for documents like protectedView Word documents or asking for Word documents like enabling editing docs 

    while using below code line.. document.Add(ref filename);

    I had try the below line code also for loading doc

    document.Open()

    But collapse ribbon functionality is not working

    Regards,

    Kiran R L

       


    R L Kiran



    • Edited by Kiran Reddi Wednesday, June 20, 2018 3:51 PM in brief
    Wednesday, June 20, 2018 3:46 PM
  • It would be Documents.Open (not Document.Open) but generally speaking, messages to do with protected mode are there to protect the user from malware. It is not always possible for code to prevent such messages appearing. If the user wishes to prevent them, they typically have to modify the security options in Word, or set the relevant documents/folders as "trusted".

    If the problem is arising because the document is set to read-only in the file system, in some cases you may be able to change the relevant setting in the file system before opening the file, using e.g.

    File.SetAttributes(fileName, FileAttributes.Normal);

    There may have been a problem in a recent "Insider" build of Word - see e.g. https://stackoverflow.com/questions/48626735/protocol-handler-broken-for-protected-mode-in-office-insider-fast-build


    Peter Jamieson

    • Marked as answer by Kiran Reddi Thursday, June 21, 2018 8:42 AM
    Wednesday, June 20, 2018 5:31 PM
  • Hi Peter ,

    Thanks for quick reply,

        After trying the below line code also 

    File.SetAttributes(fileName, FileAttributes.Normal);

    I am unable to load  documents   like protectedView Word documents or asking for Word documents like enabling editing docs .

    Error while opening doc :  [Add] | Microsoft Word.frmCompare.Add:  | Could not open macro storage.  |  System.Runtime.InteropServices.COMException (0x800A175D)

    At Line: document = wd.Documents.Add(ref fileName, ref missing, ref missing, false/true(both cases));


    R L Kiran


    • Edited by Kiran Reddi Thursday, June 21, 2018 7:29 AM
    Thursday, June 21, 2018 7:28 AM
  • If I try to done the collapse ribbon functionality after this line code

     document = wd.Documents.Open(ref fileName, ref isConfirm, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref fileFormat, ref missing, ref missing, ref missing, ref missing, ref missing);

    Able to open protectedView Word documents or asking for Word documents like enabling editing docs .

    But why the collapse ribbon functionality is not working ?


    R L Kiran


    • Edited by Kiran Reddi Thursday, June 21, 2018 2:41 PM
    Thursday, June 21, 2018 2:40 PM
  • A problem is that this Exception does not always tell you  very much. Does it happen with a lot of documents? Can you tell by opening the same document manually what the origin of the error is?

    IMO the late Shauna Kelly made a useful comment here https://social.msdn.microsoft.com/Forums/office/en-US/82c78ed6-6600-4ed8-83f4-60a49fc8491e/com-error-0x800a175d-opening-specific-doc-file?forum=worddev that

    "
    In my experience, this error almost always means that there's a broken reference in a template attached to an open document, or in an add-in (that is, a template loaded as a "global template" aka add-in). I seem to remember that back in Word 2003 days I could repro it on demand.

    The error may not come from the document in which your code is running, or the document your code is manipulating, or the template to which that document is attached. It can be any loaded template or add-in.

    If that's the case, then you just have to trap the error and tell the user that life isn't going to continue today.

    "


    Peter Jamieson

    Thursday, June 21, 2018 3:24 PM
  • Hi Peter,

      I understand the level of complication

    COM error 0x800A175D opening specific .doc file.


    Peter could a have look below code (POC) as your guidelines implemented ,the collapse functionality for Word documents for all type of files.

    For Example document used :any  Protected view document.FYI

    Facing Error like below

    NOTE : ONLY for Protected view documents only Iam facing below error ,remaining case it works fine.

    Error :  Could not open macro storage.  |  System.Runtime.InteropServices.COMException (0x800A175D)

    Word.Application wd = new Word.Application();
     object fileName = @"C:\ANY_SAMPLE_ProtectedViewDocument.doc";
     object missing = System.Reflection.Missing.Value;
                    wd.Visible = true;
                    Word.Document document=new Word.Document();
    document = wd.Documents.Add(ref fileName,ref missing, ref missing, true);
    
     document.Activate();
                    Microsoft.Office.Core.CommandBar cmdbarRibbon = wd.CommandBars["Ribbon"];
                    int theHeight = cmdbarRibbon.Height;
                    wd.CommandBars.ExecuteMso("HideRibbon");
                    if (cmdbarRibbon.Height > theHeight) // It was hidden so try minimize
                    {
                        theHeight = cmdbarRibbon.Height;
    
                        wd.CommandBars.ExecuteMso("MinimizeRibbon");
    
                    }
                    else // it is now hidden, so revert then minimize
                    {
                        wd.CommandBars.ExecuteMso("HideRibbon");
                        wd.CommandBars.ExecuteMso("MinimizeRibbon");//commented
    
                        if (cmdbarRibbon.Height > theHeight) // It was already minimized so revert
                        {
                            wd.CommandBars.ExecuteMso("MinimizeRibbon");
                            //wd.CommandBars.ExecuteMso("MinimizeRibbon");//added
                        }
                    }
                    Console.ReadKey();
    //document.Close();

    Any suggestions if I had mistakenly done..


    R L Kiran



    • Edited by Kiran Reddi Thursday, June 21, 2018 4:20 PM
    Thursday, June 21, 2018 4:02 PM
  • I am no longer sure what you are asking.

    I haven't checked your code, but the code I provided was a sample to show how you could minimise the ribbon.

    In a real-world situation you either need to anticipate exceptions and avoid them or deal with them as they occur, e.g. using try...catch...finally blocks, e.g.

    Word.Document document=new Word.Document();
    
    try
    {
        document = wd.Documents.Add(ref fileName,ref missing, ref missing, true);
    }
    
    catch (Exception e)
    {
         System.Console.WriteLine("Exception: " + e.Message);
         // maybe in some cases you could fix what is wrong here
    }
    
    finally
    {
        // in the event of an Exception, the user will probably
        // be looking at an empty document but in that case
        // code to modify the ribbon appearance should still work
        document.Activate();
        Microsoft.Office.Core.CommandBar cmdbarRibbon = wordapp.CommandBars["Ribbon"];
        // etc..
    }



    Peter Jamieson

    Friday, June 22, 2018 7:48 AM
  • Thanks Peter for reply,

    I will check it out for the issue , hope it will fix.

    Thanks and regards,

    Kiran R L


    R L Kiran

    Friday, June 22, 2018 9:26 AM
  • It got fixed thanks

    R L Kiran

    Friday, June 22, 2018 1:08 PM