Solved

VSTO Excel 2010, intercept Event 'Copy to Clipboard'

Posted on 2012-03-16
5
1,517 Views
Last Modified: 2012-03-21
Hi,
We develop a VSTO Addin for Excel 2010 and need to intercept (catch) the Event “Copy to Clipboard”  (Control-C). How to do it?
0
Comment
Question by:VN2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 19

Accepted Solution

by:
regmigrant earned 250 total points
ID: 37728909
I'm not aware of clipboard specific events but Keypress might be used to trap a 'Ctr-C', from my brief investigation I am not sure if its reliable enough for mainstream use though

reg
0
 
LVL 42

Assisted Solution

by:dlmille
dlmille earned 250 total points
ID: 37731758
Keypress might be the best bet.  I've had very good luck with my utilities using keypress, re: working with combo boxes, etc., so believe something as simple as Ctrl-C should be doable.

Other alternatives...

Just in VBA - things you can do.  If you like one and need help converting to VSTO just ask.  I think you're looking for ideas not syntax so I didn't fire up VS to just work syntax.

Having said that, there are a few additional approaches:

On worksheet selection change you can detect if ctrl-C was typed after the next selection was taken:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Application.CutCopyMode = 1 Or _
       Application.CutCopyMode = 2 Then
        MsgBox "you hit control c before that selection change!"
    End If
End Sub

Open in new window


You can intercept ribbon control copies: e.g., Control #'s 19 will capture right click copy:

Private Sub AssignMacro(ByVal strProc As String)
Dim lngId As Long
Dim CtrlCbc As CommandBarControl
Dim CtrlCbcRet As CommandBarControls
Dim arrIdNum As Variant

    ' 295 Insert Cells from worksheet menu
    ' 296 Insert rows from worksheet menu
    ' 945 Insert from right click menu

    arrIdNum = Array(19)
    For lngId = LBound(arrIdNum) To UBound(arrIdNum)
        Set CtrlCbcRet = CommandBars.FindControls(ID:=arrIdNum(lngId))
        If Not CtrlCbcRet Is Nothing Then
            For Each CtrlCbc In CtrlCbcRet
                CtrlCbc.OnAction = strProc
            Next
        End If
        Set CtrlCbcRet = Nothing
    Next

End Sub

Open in new window


Usage:  Assignmacro "macro to run if control was used"

----
I looked at OnKey methods but they require an event to trigger, so similar results to checking the cut-copy-mode approach.

Let me know if any of these look good.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37731897
Actually, OnKey works a lot better than I originally thought.  Here's the VBA code.  Test it out and let me know if you need help converting to VSTO.  I think this is the ticket!

Option Explicit

Sub test()
    Application.OnKey "^c", "RunHello"
End Sub

Sub disable()
    Application.OnKey "^c", vbNullString
End Sub

Sub reset()
    Application.OnKey "^c"
End Sub

Sub runHello()
    MsgBox "You Pressed the CTRL+c Key combo"
End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 37733356
Putting this in VSTO requires a few steps and is not as elegant as one would like.  Take a look at this thread and advise any questions:
http://social.msdn.microsoft.com/forums/en-US/vsto/thread/17915048-7049-4fdf-ba43-bfb302308d75/

Basically, the ctrl-c is trapped in the AddIn, but the sub called by the onKey event is in VBA.

Dave
0
 

Author Comment

by:VN2008
ID: 37733386
Hi,
thank you very much for tips!
Yes i need it in VSTO, not in VBA. I will check it and provide feedback.

Vladimir.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question