Solved

VSTO Excel 2010, intercept Event 'Copy to Clipboard'

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

707 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

17 Experts available now in Live!

Get 1:1 Help Now