none
Issue when Disabling Context Menu and shortcuts in Word Documents. RRS feed

  • Question

  • I need to protect a word document from cut, copy , paste , paste Special, save and save Shortcuts.

    So far I have been able to achieve my goals by using the code below:

       Imports Microsoft.Office.Interop
    
       Dim WrdApp As Word.Application
       Dim WrdDocs As Word.Documents
       Dim WrdDoc As Word.Document
       Dim cb As CommandBar
       Dim cbCtl As CommandBarControl
    
       For Each cb In WrdDoc.CommandBars
                If cb.Name <> "ClipBoard" Then
                    cbCtl = cb.FindControl(, 21, , , True) '(Cut Disabled)
                    If Not cbCtl Is Nothing Then cbCtl.Enabled = False
    
                    cbCtl = cb.FindControl(, 19, , , True) '(Copy Disabled)
                    If Not cbCtl Is Nothing Then cbCtl.Enabled = False
    
                    cbCtl = cb.FindControl(, 22, , , True)  '(paste Disabled)
                    If Not cbCtl Is Nothing Then cbCtl.Enabled = False
    
                    cbCtl = cb.FindControl(, 755, , , True) '(Paste Special Disabled)
                    If Not cbCtl Is Nothing Then cbCtl.Enabled = False
                End If
            Next
       WrdApp.FindKey(WrdApp.BuildKeyCode(Word.WdKey.wdKeyControl, word.WdKey.wdKeyS)).Disable()
         WrdApp.FindKey(WrdApp.BuildKeyCode(Word.WdKey.wdKeyControl),Word.WdKey.wdKeyC).Disable()
     WrdApp.FindKey(WrdApp.BuildKeyCode(Word.WdKey.wdKeyControl), Word.WdKey.wdKeyX).Disable()

    I have just found out that the latter code, works at the level of the entire word application and not at the level of each document; and that's my problem.

    I need to disable the context menu and save options per documents ; user can save and copy from the first document, but could not for another document.

    Is there a turn-around to this issue?

    Your help is appreciated.

    Best Regards.

    Monday, October 13, 2014 5:26 AM

Answers

All replies

  • Hello Elie,

    Command Bars are not used any longer, the Fluent UI is used instead. You can read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    Customizing Context Menus in Office 2010

    You can repurpose Ribbon controls, see Temporarily Repurpose Commands on the Office Fluent Ribbon .

    Monday, October 13, 2014 6:58 AM
  • Dear Eugene,

    I have checked out the solution you had provided, and it seems that the context menu is not customizable through CustomUI, in Word 2007 (Only Ribbon Control is customizable).

    I have found the following documentation, but it only works on word 2010 : http://msdn.microsoft.com/en-us/library/ee691832.aspx.

    In case there are no alternatives,please consider checking my problem and providing any solution :
    My purpose is to lock a word document from copy, cut , paste, paste special , save, and disable the related shortcuts.

    Any help is much appreciated.

    Regards

    Elie.

    Monday, October 13, 2014 8:18 AM
  • Elie,

    When working with the Ribbon you have to re-think your approach. As I wrote, you'd need to repurpose the command.

    The Modify Commands in Existing Excel Shortcut Menues (Excel 2010) forum thread states the following:

    Repurpose: put a <commands> section in your Ribbon XML. You an assign a callback to the onAction attribute of the command, so that your code executes instead (no matter how the command is called in the application). You can choose to disable it from the start, or disable it dynamically (getEnabled).

    Or you can write RibbonXML for that context menu, use the idMso of this button control to define the button, and make it invisible or disable it. You can then define your own button that performs the code you require.

    Monday, October 13, 2014 9:12 AM
  • Hi elie,

    First, please refer to Eugene’s reply.

    Secondly, there is a related thread that can help you (It works for me in office 2007):

    # How to Disable the buttons Cut, Copy, Paste and Format from Ribbon Clipboard Group

    https://social.msdn.microsoft.com/Forums/office/en-US/145c2213-770f-42fe-bd54-94260584d654/excel-2007-how-to-disable-the-buttons-cut-copy-paste-and-format-from-ribbon-clipboard-group?forum=exceldev

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 14, 2014 6:11 AM
    Moderator
  • Dear Eugene , Starain

    I would like to thank you for your help. So far I was able to disable the Context Menus, but not their shortcut keys, plus , I was able to disable the save button from the ribbon, but not its related shortcut.

    In order to disable shortcuts, I have improvised a solution that suits my case, however, a single issue remains.

    Please consider following my approach:

    1-Using Custom UI i have overridden the save functionality of a word document.

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
       onLoad="OnLoad" >
       <commands>
         <command idMso="FileSave" onAction="mySave" />
       </commands>
       </customUI>


    2- I have written a vba macro within the the word document, my problem is here, I don't know what are the parameters for the Subroutine.

    When I Try this syntax:

    Public Sub MySave(control As IRibbonControl)
        MsgBox "HELLO"
    End Sub

    I get this message On save :

    Wrong Number Of Arguments Or Invalid Property Assignement


    When I Try This Syntax :

    Public Sub MySave(control As IRibbonControl, ByRef cancelDefault As Boolean)
        MsgBox "HELLO"
    End Sub

    I get this message on save :

    Type Mismatch

    When I Try This Syntax :

    Public Sub MySave(control As IRibbonControl, ByRef cancelDefault As Boolean)
        MsgBox "HELLO"
    End Sub

    I get this message on save :

    Wrong Number of Arguments or Invalid Property Assignment.


    Tuesday, October 14, 2014 8:36 AM
  • Hello Elie,

    It looks like you didn't see articles I mentioned in my first reply there. You can read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    The last link provides the following information, for example, for a button control:

    button

    onAction – repurposed

    C#: void OnAction(IRibbonControl control, ref bool CancelDefault)

    VBA: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

    C++: HRESULT OnAction([in] IRibbonControl *pControl, [in,out] VARIANT _BOOL *fCancelDefault)

    Visual Basic: Sub OnAction(control As IRibbonControl, byRef CancelDefault)

    > So far I was able to disable the Context Menus, but not their shortcut keys

    VSTO (nor Office) doesn't provide anything for intercepting shortcut keys. You need to implement a keyboard hook using Windows API functions on your own - SetWindowsHookEx. See Using shortcut keys to call a function in an Office Add-in and Office Hook Key Shortcut .

    Tuesday, October 14, 2014 11:29 AM
  • Hi elie,

    To deal with the save event, you could capture the DocumentBeforeSave event.

    For example:

    Public WithEvents appWord As Word.Application
    
    Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
    Dim intResponse As Integer
     
     intResponse = MsgBox("Do you really want to " _
     & "save the document?", _
     vbYesNo)
     
     If intResponse = vbNo Then Cancel = True
    End Sub

    More information, please refer to:

    # Application.DocumentBeforeSave Event (Word)

    http://msdn.microsoft.com/en-us/library/office/ff838299(v=office.14).aspx

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 15, 2014 2:23 AM
    Moderator