Solved

VSTO Excel 2010, intercept Event 'Copy to Clipboard'

Posted on 2012-03-16
5
1,480 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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