none
Get Content Type Properties on Ribbon Load RRS feed

  • Question

  • I am trying to get the Content Type Properties of Word Document (opened from SharePoint document library) in to the ribbon load event, but I am ending up with the error message saying that - 

    This command is not available because no document is open.

    I need to show / hide some buttons in my custom ribbon based on certain conditions which needs to check the metadata associated with the document.

    I am doing like - 

    MetaProperties _metaProperties = Globals.ThisAddIn.Application.ActiveDocument.ContentTypeProperties;
    

     

    Any suggestion that how this can be checked on the ribbon load event?

     


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Monday, December 19, 2011 7:38 AM

Answers

  • Then the document is opened by the time the add-in gets loaded by Word. Just to be sure that the event works for you, start Word via VS and then open a document via Word UI.

    To solve your original task, I suggest that you check if WindowActivate occurs for you.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    • Marked as answer by Bhushan Gawale Friday, December 23, 2011 2:24 PM
    Tuesday, December 20, 2011 10:36 AM

All replies

  • Hello Bhushan,

    It looks like you can't achieve this because the custom Ribbon is loaded before a document is opened. I suggest that you intercept the DocumentOpen or even WindowActivate event to show/hide your controls; all the controls must be created when the Ribbon is being loaded.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Monday, December 19, 2011 7:48 AM
  • Hi Andrei,

    Thanks for the quick reply.

    Yes I guess you are right , I am new to the Add Ins Development but logical thought came to my mind was the same , as ribbon gets loaded before the document.

    Could you please give any pointer on how to intercept the Document Open event? or any links for reference?

     


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Monday, December 19, 2011 7:51 AM
  • Adding the following and your custom code to this routine in your project should work.

        Private Sub Application_DocumentOpen(ByVal Doc As Microsoft.Office.Interop.Word.Document) Handles Application.DocumentOpen
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Monday, December 19, 2011 12:52 PM
  • Thank you, Rich!

    Bhushan,

    The C# version of the same code is:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    }
    
    void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
    {
        throw new NotImplementedException();
    }
    
    Note that VS created it for me: I've printed "this.Application.DocumentOpen +=" and it suggests pressing {Tab} to add "new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);" and then it suggests pressing another {Tab} to create the method Application_DocumentOpen. You can create an event handler for the WindowActivate event in exactly the same way.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Monday, December 19, 2011 7:16 PM
  • Thanks for the reply Andrei,

    I tried using the way you suggested and debug but Application_Document Event didn't trigger.

     

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    }
    
    void Application_DocumentOpen(Word.Document Doc)
    {
          //This Event Never Turn Up
    }
    
    Any Clue?

     


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale

    Tuesday, December 20, 2011 9:16 AM
  • Have you created the code by copying it or allowing VS to create it for you?

    I suppose you start Word by double-clicking a document. Is it correct? Does it work if you open a document when Word is already started?


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, December 20, 2011 9:41 AM
  • Basically I am trying to create a word application level add in so VS created the code for me , I just added the event which you mentioned above.

    I am starting the word app using VS (when you run the add in code) , for debugging purpose but the event didnt trigger.


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Tuesday, December 20, 2011 10:15 AM
  • Then the document is opened by the time the add-in gets loaded by Word. Just to be sure that the event works for you, start Word via VS and then open a document via Word UI.

    To solve your original task, I suggest that you check if WindowActivate occurs for you.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    • Marked as answer by Bhushan Gawale Friday, December 23, 2011 2:24 PM
    Tuesday, December 20, 2011 10:36 AM
  • Thanks Andrei for the help 

    I guess document change event is useful to get the content type properties on ribbon load.


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Friday, December 23, 2011 2:23 PM