Solved

VSTO Excel 2010, intercept Event 'Copy to Clipboard'

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

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 41

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 41

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now