Consulting

Results 1 to 3 of 3

Thread: Overriding Disable copy/paste code

  1. #1

    Overriding Disable copy/paste code

    Does anyone have the VBA code to override the following code and to enable the copy paste function. Thank you.

    '*** In a standard module ***
    Option Explicit
     
    Sub ToggleCutCopyAndPaste(Allow As Boolean)
         'Activate/deactivate cut, copy, paste and pastespecial menu items
        Call EnableMenuItem(21, Allow) ' cut
        Call EnableMenuItem(19, Allow) ' copy
        Call EnableMenuItem(22, Allow) ' paste
        Call EnableMenuItem(755, Allow) ' pastespecial
         
         'Activate/deactivate drag and drop ability
        Application.CellDragAndDrop = Allow
         
         'Activate/deactivate cut, copy, paste and pastespecial shortcut keys
        With Application
            Select Case Allow
            Case Is = False
                .OnKey "^c", "CutCopyPasteDisabled"
                .OnKey "^v", "CutCopyPasteDisabled"
                .OnKey "^x", "CutCopyPasteDisabled"
                .OnKey "+{DEL}", "CutCopyPasteDisabled"
                .OnKey "^{INSERT}", "CutCopyPasteDisabled"
            Case Is = True
                .OnKey "^c"
                .OnKey "^v"
                .OnKey "^x"
                .OnKey "+{DEL}"
                .OnKey "^{INSERT}"
            End Select
        End With
    End Sub
     
    Sub EnableMenuItem(ctlId As Integer, Enabled As Boolean)
         'Activate/Deactivate specific menu item
        Dim cBar As CommandBar
        Dim cBarCtrl As CommandBarControl
        For Each cBar In Application.CommandBars
            If cBar.Name <> "Clipboard" Then
                Set cBarCtrl = cBar.FindControl(ID:=ctlId, recursive:=True)
                If Not cBarCtrl Is Nothing Then cBarCtrl.Enabled = Enabled
            End If
        Next
    End Sub
     
    Sub CutCopyPasteDisabled()
         'Inform user that the functions have been disabled
        MsgBox "Sorry!  Cutting, copying and pasting have been disabled in this workbook!"
    End Sub

    Private Sub Workbook_Activate()
        Call ToggleCutCopyAndPaste(False)
    End Sub
     
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Call ToggleCutCopyAndPaste(True)
    End Sub
     
    Private Sub Workbook_Deactivate()
        Call ToggleCutCopyAndPaste(True)
    End Sub
     
    Private Sub Workbook_Open()
        Call ToggleCutCopyAndPaste(False)
    End Sub
    Last edited by Paul_Hossler; 03-07-2017 at 07:50 AM. Reason: Added [CODE] tags - use the [#] to add [CODE] ... [/[CODE]

  2. #2
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    have you tried changing the code in the workbook_activate() module to:

    [VBA]Private Sub Workbook_Activate()
    Call ToggleCutCopyAndPaste(TRUE)
    End Sub[/VBA]

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    or just delete these if you don't want to use block copy/paste at all

    Private Sub Workbook_Activate() 
        Call ToggleCutCopyAndPaste(False) 
    End Sub 
     
    Private Sub Workbook_BeforeClose(Cancel As Boolean) 
        Call ToggleCutCopyAndPaste(True) 
    End Sub 
     
    Private Sub Workbook_Deactivate() 
        Call ToggleCutCopyAndPaste(True) 
    End Sub 
     
    Private Sub Workbook_Open() 
        Call ToggleCutCopyAndPaste(False) 
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •