none
IRibbonControl.Context for a protected document RRS feed

  • Question

  • Hi;

    I'm diving in to how the ribbon passes the window the ribbon is for. For a non-protected document it appears to work as expected. But for a protected document.

    1. In Excel IRibbonControl.Context is a ProtectedViewWindow - what I expected.

    2. In Word IRibbonControl.Context is null. This is a problem because for no document it is also null. So how do I determine what, if any, document a menu event is for if I have a protected document?

    3. PowerPoint is like Word.

    thanks - dave


    Very funny video - What's your Metaphor?
    Monday, March 7, 2011 7:03 PM

All replies

  • Hello Dave,

    Thanks for posting. From the MSDN page about Context property, it statements as below,

    Represents the active window containing the Ribbon user interface that triggers a callback procedure. Read-only.

    Also I have made a test in Word 2007. In OnAction callback of a button which is added via Ribbon XML, use IRibbonControl.Context. It will also return an object whether the document is protected or not.

    If there is a difference between us, or I have mistaken any point, just feel free to let me know. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 8, 2011 4:06 AM
  • Hi;

    Two things. First off I am on Office 2010 - this feature is new to Office 2010.

    Second, by "protected" I mean the Office 2010 mode where a file that is downloaded from the internet cannot be edited until you click the enable button. This is different from a document that is protected where the user has marked it to not be editable.

    Can you try with Word 2010 and a Word document you download from the internet and then open?

    thanks - dave


    Very funny video - What's your Metaphor?
    Tuesday, March 8, 2011 4:30 AM
  • Hello again Dave,

    Thanks for the further information. I will test it in a machine with Office 2010 installed. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 8, 2011 6:12 AM
  • Thanks for your further information. Now I am able to see this issue on my side by creating a Word add-in. From the result of the test, there is no an ActiveDocument when the document is opened in Protected View. After Enable Editing button, the document will be initialized. The project I used is here.

    Unfortunately, I am not able to find a solution for this issue. If you like, I will report a feedback for this issue. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 9, 2011 9:34 AM
  • Please escalate this and ask how we are supposed to handle this. I am getting IRibbonControl messages for getVisible and getEnabled and with a null Context I don't know what document that menu message is for.

    I must have this information to properly display my ribbon buttons.

    thanks - dave


    Very funny video - What's your Metaphor?
    Wednesday, March 9, 2011 4:41 PM
  • By doing a lot of research, I find that the method listed in this blog would helpful for this issue: http://blogs.office.com/b/microsoft-word/archive/2010/09/13/object-model-considerations-for-protected-view-in-word-2010.aspx. It says as below,

    Word 2010 includes enhancements to security, one of which is the inclusion of a Protected View used when opening files from untrusted sources (e.g. email attachments). The inclusion of Protected View can require changes to the way that solutions interact with Word documents, as some object model methods/properties are disabled while in Protected View.

    I think it could explain the scenario that IRibbonControl.Context is null in protected view. It also introduces that we could access the Document property on the ActiveProtectedViewWindow object. So I think we could consider using this method instead of Context.

    If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    Thursday, March 10, 2011 9:05 AM
  • I think it could explain the scenario that IRibbonControl.Context is null in protected view. It also introduces that we could access the Document property on the ActiveProtectedViewWindow object. So I think we could consider using this method instead of Context.

    Hi Bessie;

    Please do the following. Open two Word windows - one has a document from the internet (protected), and one has no document (empty).

    I now get an event for the ribbon menu and the Context is null. Which Word window is it for?

    Please escalate this question to the Word development team as the present situation makes it impossible to determine which document a ribbon call is for.

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 10, 2011 4:46 PM
  • Hi Dave,

    Where is your code firing from when you get a null value? I'm not exactly sure I understand how you have this setup.

    The only thing I can think of to try is to use the ProtectedViewWindows object. You could check the count and whatever window has the protected view you can get the caption from that window. Is that what you mean by getting the context? If that isn't what you are looking for I might need a sample of how you are using the context feature of IRibbonControl to try and get you a better idea.

    The following is a very quick sample of how I get the document that is currently open but in protected view from a button in the ribbon.

    Sub Callback(control As IRibbonControl)
    Dim pvWindow As ProtectedViewWindow

    If Application.ProtectedViewWindows.Count > 0 Then
    Set pvWindow = Application.ProtectedViewWindows(1)
        If pvWindow.Active = True Then
            MsgBox pvWindow.Caption
        Else
            MsgBox "Callback " & control.Context
        End If
    End If
    End Sub

     

    Brandon

    Friday, March 11, 2011 11:33 PM
    Moderator
  • Hi Brandon;

    Here's where we've found this. In our ribbon XML we have numerous getVisible='method' and getEnabled='method' attributes for various buttons. The methods have the signature:

    public bool MyIsEnabled(IRibbonControl control)
    {
    Microsoft.Office.Interop.Word.Window winWord = control.Context as Microsoft.Office.Interop.Word.Window;
    // now determin enabled based on the winWord.Document we were passed	
    }
    
    

    The problem is that in Word (and PPT), if the request is for a protected window, then control is null. For Excel it is Microsoft.Office.Interop.Excel.ProtectedViewWindow so I'm guessing Word & PPT should have done that.

    If Word has no document, then it also passes null for control. That does make sense. But it leaves the question, if we receive a null and we have two Word windows up, one protected and one empty - which window is it?

    Keep in mind that Word does call the getVisible/Enabled methods for non-active windows.

    ??? - thanks - dave


    Very funny video - What's your Metaphor?
    Saturday, March 12, 2011 10:54 PM
  • Hey Dave,

    Can you try using the IsSandboxed property to check if the document is protected instead of what you have above? 

    Brandon

    • Marked as answer by DavidThi808 Wednesday, March 23, 2011 11:40 PM
    • Unmarked as answer by DavidThi808 Thursday, March 24, 2011 4:41 PM
    Tuesday, March 22, 2011 9:03 PM
    Moderator
  • That's it!!!!!

    thank you.


    Very funny video - What's your Metaphor?
    Wednesday, March 23, 2011 11:40 PM
  • Hi;

    We still have a problem where we don't know if a menu event is for the protected document, or for the window with no document. How do we determine that?

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 24, 2011 4:42 PM
  • What do you mean by menu event?

    Brandon

    Thursday, March 24, 2011 6:00 PM
    Moderator
  • Hi;

    Back to the start of this thread (it did evolve in the discussion). For a ribbon menu, you can have methods called against getEnabled and getVisible. These methods are passed a IRibbonControl.Context which is the Window the ribbon is in.

    The Context is key because you can get events for non-active windows. So we need to know which Window the event is for.

    If you have no document in a window Context is null - which makes sense. But if the document is sandboxed, then Context is also null. So if we have to sandboxed documents opened - which is the event for?

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 24, 2011 7:14 PM
  • Hey David,

    I think the following will get you the behavior you are looking for.  It sounds like you want to know if the callback being used is for a protected document or non protected document.  The following sample seems to be working on my machine, does this look like something you can implement?  I'm not taking into account getEnabled or getVisible but it should still work, you just won't be able to use context when there are protected windows open in the application.

    Sub Callback(control As IRibbonControl)
        If Application.ProtectedViewWindows.Count > 0 Then
            Dim pvWindow As ProtectedViewWindow
            Dim regularWindow As Window
               
            For Each pvWindow In Application.ProtectedViewWindows
                If pvWindow.Active = True Then
                    MsgBox "Protected Document " & pvWindow.Caption
                End If
            Next pvWindow
           
            For Each regularWindow In Application.Windows
                If regularWindow.Active = True Then
                    MsgBox "Un-Protected " & regularWindow.Caption
                End If
            Next regularWindow
        Else
            MsgBox "Un-Protected " & control.Context
        End If
    End Sub

    If that doesn't work, I apologize for the confusion and the only suggestion I have is to open a case with Microsoft Support and they can get you something more specific to what you want.

    Brandon

    Thursday, March 24, 2011 9:52 PM
    Moderator
  • Hi;

    That works if there is 1 protected document open. But it does not work if there are two or more. I'll put in a support ticket (those guys hate me - because they can never answer my questions).

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 24, 2011 10:13 PM
  • Interesting, I thought for sure it would work.  I tested with 4 documents (2 protected and 2 unprotected) open at the same time and they all gave me the correct active document name. 

    Brandon

    Thursday, March 24, 2011 11:33 PM
    Moderator
  • Hi;

    Getting the active protected window can be done. The problem is sometimes the menu events are for a non-active window. That's where this becomes essential. (It rarely happens but when it does, if the menus need to be different, it causes the menu to be wrong.)

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, March 25, 2011 2:06 AM