Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > Make add-in ribbon visible only to some files
Ask a questionAsk a question
 

QuestionMake add-in ribbon visible only to some files

  • Friday, October 30, 2009 3:28 PMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is there a way to make my ribbon visible only to some files?

    I have an add-in (excel,word,powerpoint - you're choice) that has a group with some buttons. On some files this group is visible and on some files is not. I have tried with Ribbon.PerformLayout but it does not work.

All Replies

  • Saturday, October 31, 2009 12:04 AMStephen Peters - MSFTMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    If the Ribbon controls will only be visible to a very small number of files, you might want to consider adding the Ribbon to a Document level addin for those Documents.

    If you want to dynamically control if a group is visible or not, you can do that by just setting the "Visible" property of the group to either true or false.  To do this from the addin, consider the following snippet:

    Ribbon1 r1 = (Ribbon1)Globals.Ribbons.GetRibbon(typeof(Ribbon1));
    r1.group1.Visible = false;

  • Monday, November 02, 2009 6:36 AMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Where is the method GetRibbon?

    How is this different from

    Globals.Ribbons.Ribbon1.group1.Visible = false;

    Witch doesn't work?
  • Monday, November 02, 2009 3:28 PMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is there a way to make my ribbon visible only to some files?

    I have an add-in (excel,word,powerpoint - you're choice) that has a group with some buttons. On some files this group is visible and on some files is not. I have tried with Ribbon.PerformLayout but it does not work.

    Which version of VSTO are you using? Are you working with the Ribbon Designer, or Ribbon XML?

    You'd need to use an event in the application - which event will depend on which application - that triggers when the file is changed (DocumentChange in Word, for example). At that point, your code needs to force the Ribbon.Invalidate method, which will cause the Ribbon to re-evaluate all getVisible and other dynamic callbacks.

    If you want code samples, we need answers to my questions, above.
    Cindy Meister, VSTO/Word MVP
  • Monday, November 02, 2009 4:37 PMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    he version of VSTO is 3.0 and i created the ribbon with the Ribbon Designer.

    I used Ribbon.Invalidate but still my ribbon remained invisible.
    This is the reason i'm asking.

    Let me explain what I have.

    A group and some button on the group on the addin ribbon.
    I open a file that has the group visible on the addin ribbon. Then i open another file that should not have the group visible. After activating sequentialy the two files at some point the ribbon will not make it self visible even though I make the group visible. Also after i make the group visible/invisible i call the PerformLayout method.

    Is this what should I do:

    Globals.Ribbons.Ribbon1.RibbonUI.Invalidate(); instead of PerformLayout



  • Tuesday, November 03, 2009 8:14 AMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The Globals.Ribbons.Ribbon1.RibbonUI.Invalidate() is not working either
  • Tuesday, November 03, 2009 8:23 AMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    <<I used Ribbon.Invalidate but still my ribbon remained invisible.
    This is the reason i'm asking.

    Let me explain what I have.

    A group and some button on the group on the addin ribbon.
    I open a file that has the group visible on the addin ribbon. Then i open another file that should not have the group visible. After activating sequentialy the two files at some point the ribbon will not make it self visible even though I make the group visible. Also after i make the group visible/invisible i call the PerformLayout method.>>

    So, sometimes it works and other times it does not?

    Please copy the relevant code sections (using the "Insert Code Block" tool in the toolbar) into your reply so that we can see exactly how things are working and test on our side.
    Cindy Meister, VSTO/Word MVP
  • Tuesday, November 03, 2009 8:42 AMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Here some code

    public void UpdateButtons()
    {
    	// here you have to put you're condition<br/>	// one solution would be the name of the active presentation to be a specific file<br/>	// or you could try when no presentation is opened
    	if (currentPresentation == null)
    		Globals.Ribbons.AutomaticPrintRibbon.HideAddIn();
    	else
    		Globals.Ribbons.AutomaticPrintRibbon.ShowHideButtons();<br/>	// uncomment one of these
    	//Globals.Ribbons.AutomaticPrintRibbon.PerformDynamicLayout();
    	//Globals.Ribbons.AutomaticPrintRibbon.RibbonUI.Invalidate();
    }
    
    This method is called from
    -excel => WorkbookActivate
    -word => DocumentChange
    -power => WindowActivate (because there is no other event for what i need)

    This code goes in the ribbon

    internal void HideAddIn()
    {
    	grpAutomaticPrint.Visible = false;
    	btnPrint.Enabled = false;
    }
    
    internal void ShowHideButtons()
    {
    	grpAutomaticPrint.Visible = true;
    	btnPrint.Enabled = true;
    }
    
  • Wednesday, November 04, 2009 7:23 AMBessie ZhaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    It seems that you are working with PowerPoint. For test, in my side, I have made a Word Add-in add-in. When I put the code Globals.Ribbons.Ribbon1.group3.Visible = false;
    into the DocumentChange event handle. When I switch the documents, this code can be executed. The code I used can hide the group on the tab.

    <<sometimes it works and other times it does not?>>
    If Cindy is right, I think you could set breakpoint to code, and debug it step by step. It seems that the code above is little part.  Now try it again. Does it work fine?

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 04, 2009 8:08 AMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The problem is not that it doesn't make it false because it does.

    My problem is that after is made false sometimes the ribbon appears and some times it doesn't even though the group is visible.
  • Wednesday, November 04, 2009 9:17 AMBessie ZhaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Sorry for mistaking this scenario. Regarding this issue, I am thinking it may be caused some code in if-else block has been executed wrongly. Since I am not sure the condition by which the ribbon is visible/ invisible on some documents, it is just my guess and the condition may be changed.

    I know it is a little complicated to debug it. However, I still suggest that you could set breakpoints to your code, and check if the condition is changed or the code is executed in if-else block.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 04, 2009 5:33 PMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Offcores I have debug-it and i know that the code is executed.

    I haven't tested so i'm not sure if it will work, but tomorrow i will tested to see if it's reproducible.

    Change the code in UpdateButtons from my previous post into

    public void UpdateButtons()
    {
    	if (Application.Documents.Count == 0)
    		Globals.Ribbons.AutomaticPrintRibbon.HideAddIn();
    	else
    		Globals.Ribbons.AutomaticPrintRibbon.ShowHideButtons();
    	// uncomment one of these
    	//Globals.Ribbons.AutomaticPrintRibbon.PerformDynamicLayout();
    	//Globals.Ribbons.AutomaticPrintRibbon.RibbonUI.Invalidate();
    }
    call this method from the DocumentChange.

    Open a document. The Ribbon Add-In is visible. Close the document(but not the word process). The Ribbon Add-In should not be visible. Now open a document or create a new one. The Ribbon Add-In i think will not be visible, even thou the code of ShowHideButtons was executed.
    • Edited byARHANGEL19 Thursday, November 05, 2009 7:51 AM
    •  
  • Thursday, November 05, 2009 7:50 AMARHANGEL19 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I just tested and i reproduce the problem.
  • Thursday, November 05, 2009 10:19 AMBessie ZhaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    It seems that you have found the cause. Have you got the solution?

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.