none
Ribbon (Visual Designer): How to show/hide tabs from ThisAddIn RRS feed

  • Question

  • vsto 2010, Word 2010, Ribbon (Visual Designer): How to show/hide tabs from ThisAddIn

    Two tabs have been added to a Word Ribbon (Odd & Even)
    Each contains one button that toggles the other tabs visibility.

    How can I control the tabs visibility from ThisAddIn class?


    ***********************************************************************************
    Public Class ThisAddIn

      Private Sub ThisAddIn_Startup() Handles Me.Startup
        Debug.Print("ThisAddIn_Startup")
        'ShowHideTabs(Doc:=Application.ActiveDocument)
      End Sub

      Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        Debug.Print("ThisAddIn_Shutdown")
      End Sub

      Private Sub Application_DocumentBeforeSave(ByVal Doc As Word.Document, ByRef SaveAsUI As Boolean, _
            ByRef Cancel As Boolean) Handles Application.DocumentBeforeSave
        Debug.Print("Name:" & Doc.Name & ", BeforeSave")
      End Sub

      Private Sub Application_DocumentOpen(ByVal Doc As Microsoft.Office.Interop.Word.Document) _
          Handles Application.DocumentOpen
        Debug.Print("Name:" & Doc.Name & ", DocumentOpen")
        'ShowHideTabs(doc:=Doc)
      End Sub

      Private Sub Application_NewDocument(ByVal Doc As Microsoft.Office.Interop.Word.Document) _
          Handles Application.NewDocument
        Debug.Print("Name:" & Doc.Name & ", NewDocument")
        'ShowHideTabs(doc:=Doc)
      End Sub

      Private Sub ShowHideTabs(ByVal Doc As Microsoft.Office.Interop.Word.Document)
        Dim RibD As New RibbonDesigned
        If IsNumeric(Doc.Name.Last) Then
          If Val(Doc.Name.Last) Mod 2 = 0 Then   'Ends with Even Number
            RibD.TabEven.Visible = True
            RibD.TabOdd.Visible = False
          Else
            RibD.TabEven.Visible = False
            RibD.TabOdd.Visible = True
          End If
        Else
          RibD.TabEven.Visible = True
          RibD.TabOdd.Visible = True
        End If
      End Sub

      Private Sub Application_Quit() Handles Application.Quit
        Debug.Print("Application_Quit")
      End Sub

      Private Sub Application_Startup() Handles Application.Startup
        Debug.Print("Application_Startup")
      End Sub

      Private Sub Application_WindowActivate(ByVal Doc As Microsoft.Office.Interop.Word.Document, _
          ByVal Wn As Microsoft.Office.Interop.Word.Window) Handles Application.WindowActivate
        Dim Template As Word.Template
        Template = TryCast(Doc.AttachedTemplate, Word.Template)
        Debug.Print("Name:" & Doc.Name & ", Template:" & Template.Name & ", WindowActivate")
        'ShowHideTabs(Doc:=Doc)
      End Sub

    End Class

    ***********************************************************************************
    Imports Microsoft.Office.Tools.Ribbon

    Public Class RibbonDesigned

      Private Sub RibbonLoad(ByVal sender As System.Object, _
          ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        Debug.Print("Ribbon loaded")
      End Sub

      Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button2.Click
        TabOdd.Visible = Not TabOdd.Visible
      End Sub

      Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
        TabEven.Visible = Not TabEven.Visible
      End Sub

      Public Sub HideAll()
        TabOdd.Visible = False
        TabEven.Visible = False
      End Sub

    End Class

     


    Tim
    Tuesday, August 9, 2011 3:51 PM

Answers

  • Hi Tim

    I'd like to move this to the VSTO forum, if that's OK with you, as the question definitely falls in the VSTO realm of functionality...

    Normally, I'd expect the default property settings to be correct for when the Ribbon starts (IOW you wouldn't need to call something from ThisDocument_Startup). If that needs to be dynamic, then I'd expect that in the RibbonLoad event code.

    And for the part in WindowActivate I'd call a method in the Ribbon class. If you don't want to do that, then you should be able to access the Ribbon object via Gloabls.Ribbon...


    Cindy Meister, VSTO/Word MVP
    Tuesday, August 9, 2011 4:14 PM
    Moderator

All replies

  • Hi Tim

    I'd like to move this to the VSTO forum, if that's OK with you, as the question definitely falls in the VSTO realm of functionality...

    Normally, I'd expect the default property settings to be correct for when the Ribbon starts (IOW you wouldn't need to call something from ThisDocument_Startup). If that needs to be dynamic, then I'd expect that in the RibbonLoad event code.

    And for the part in WindowActivate I'd call a method in the Ribbon class. If you don't want to do that, then you should be able to access the Ribbon object via Gloabls.Ribbon...


    Cindy Meister, VSTO/Word MVP
    Tuesday, August 9, 2011 4:14 PM
    Moderator
  • Cindy,

    I agree the VSTO forum is more appropriate.

    My long term intention is to be able to expose different tabs based 
    upon the template of the active document. Based upon your response,
    sounds like I need to get familiar with Globals.Ribbon...

    Any links would be greatly appreciated.

    Tim


    Tim
    Tuesday, August 9, 2011 4:26 PM
  • Thanks Cindy,

    I ended utilizing WindowActivate.

    Appears to do what I want.

    If a document name ends with an even number, the Even tab is visible and
    if the document name ends with a old number, the Old tab is visible.

    If I open up multiple documents and use Arrange All. The ribbon for each
    document shows the correct tab.

    Thanks 

    ************************************************

      Private Sub Application_WindowActivate(ByVal Doc As Microsoft.Office.Interop.Word.Document, _
          ByVal Wn As Microsoft.Office.Interop.Word.Window) Handles Application.WindowActivate
        Dim Template As Word.Template
        Template = TryCast(Doc.AttachedTemplate, Word.Template)
        Debug.Print("Name:" & Doc.Name & ", Template:" & Template.Name & ", WindowActivate")
        ShowHideTabs(Doc:=Doc)
      End Sub

      Private Sub ShowHideTabs(ByVal Doc As Microsoft.Office.Interop.Word.Document)
        If IsNumeric(Doc.Name.Last) Then
          If Val(Doc.Name.Last) Mod 2 = 0 Then   'Ends with Even Number
            Globals.Ribbons.RibbonDesigned.TabEven.Visible = True
            Globals.Ribbons.RibbonDesigned.TabOdd.Visible = False
          Else
            Globals.Ribbons.RibbonDesigned.TabEven.Visible = False
            Globals.Ribbons.RibbonDesigned.TabOdd.Visible = True
          End If
        Else
          Globals.Ribbons.RibbonDesigned.TabEven.Visible = True
          Globals.Ribbons.RibbonDesigned.TabOdd.Visible = True
        End If
      End Sub

     


    Tim
    Tuesday, August 9, 2011 5:43 PM
  • Hi Tim

    We sort of crossed, here... Glad you've got something that's working :-)

    I unmarked the "Answer" before you posted your solution, so you'll need to remark.

    And I've moved the message to the VSTO forum.


    Cindy Meister, VSTO/Word MVP
    Tuesday, August 9, 2011 5:45 PM
    Moderator