Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VSTO Excel 2010, intercept Event 'Copy to Clipboard'

Posted on 2012-03-16
5
Medium Priority
?
1,601 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 1000 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

618 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