C# how can I find out if the ribbon is currently visible or not RRS feed

  • Question

  • Using C#

    I'm launching Word from my C# application.
    //Create a new document from template
    Object oTemplatePath = GetTemplateFile();
    m_document = m_winword.Documents.Add(ref oTemplatePath, ref missing, ref missing, ref missing);

    I will like to hide the ribbon in Word only if the ribbon is currently visible. I know I can use ActiveWindow.ToggleRibbon(); to Show or hide the ribbon. However how can I find out if the ribbon is currently visible or not ?


    Wednesday, May 21, 2014 4:41 PM


  • Hi MCunha

    Here's a bit of VBA code I wrote and tested a (longish) while back for this requirement. Basically, it checks the size of the application window vs. the size available for the document window. If the number is larger than a certain amount, the Ribbon is showing, otherwise it's not. You'll want to test it, of course.

    You shouldn't have too much trouble converting it to C#, just keep in mind that Selection, ActiveDocument and ActiveWindow all depend from the Word.Application object (m_winword in your code). And for the Collapse method you have to pass the WdCollapseDirection enum member as an object, using the keyword ref

    Sub TurnRibbonOff()
        Dim win As word.Window
        Dim pLeft As Long, pTop As Long, pHeight As Long, pWidth As Long
        Dim rngSel As word.Range, rngDocTop As word.Range, oRng As Object
        Set win = ActiveWindow
        Set rngSel = Selection.Range
        Set rngDocTop = ActiveDocument.content
        rngDocTop.Collapse wdCollapseStart  'Top of the document
        Set oRng = rngDocTop
        win.LargeScroll Up:=10 'Force the maximum scroll
        win.GetPoint pLeft, pTop, pHeight, pWidth, oRng
        Debug.Print win.height, Application.UsableHeight, pLeft, pTop, pHeight, pWidth
        If pTop > 300 Then
        End If
    End Sub

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by MCunha Thursday, May 22, 2014 3:42 PM
    Thursday, May 22, 2014 2:11 PM