Home » excel » vba – Disable Ctrl+V paste option from Excel template in VSTO

vba – Disable Ctrl+V paste option from Excel template in VSTO

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am working on an Excel template + VSTO application.
I have assigned various custom validations and formats for various cells.

But whenever I copy and paste something into the cell, these validations do not work (fails completely). Is there any way so I can disable (Ctrl + V) paste options from the Excel template?

I think it can be implemented in VBA macros.
But I’m not sure about it.

How to&Answers:

Have you tried using Application.OnKey? The following code should intercept Ctrl+V and effectively disable it. You should call it when your template opens.

Application.OnKey("^v", "");

If you would like to restore Ctrl+V, call the following:

Application.OnKey("^v", Type.Missing);

In VBA you can put the name of a sub in the second parameter, which will be run when that key is pressed. I’m not sure how that works in VSTO.

Of course, the user could still use the cell menu or the edit menu to paste. Also, be aware that this will affect all workbooks running in the Excel instance.