Restrict Pasting Into Textbox

sachin.acharya

Board Regular
Joined
Nov 5, 2009
Messages
93
Hi Gurus..

I have a textbox where I want user to only type in manually and not copy any text from somewhere and paste it into the textbox.

How do I disable the paste option when the cursor is in a textbox.

This is what I tried and the reason it failed:

Private Sub txtPassword_Enter()
Call ClearClipboard
End Sub

Sub ClearClipboard()
Dim strDO As DataObject
Set strDO = New DataObject
With strDO
.SetText ""
.PutInClipboard
End With
End Sub

Why it failed? Well it clears the clipboard when the user "Enters" the textbox. If the cursor is already in the textbox, it doesn't help.

Any help is appreciated.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Have you tried setting the .DragBehaviour property to fmDragBehaviourDisabled?

Have you tried calling ClearClipboard from the UserForm_Activate event?

On a more general note, wouldn't allowing copy pasting prevent misspellings?
 
Upvote 0
This will prevent CTRL-V pasting.

Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If Shift = 2 Then KeyCode = 0
End Sub

If your textbox has a context menu, place your code in the mousedown event.
 
Upvote 0
Thanks for the suggestions.

I used...

Application.OnKey "^V", ""

...while loading the form and then...

Application.OnKey "^V"

...while unloading it.

It serves the purpose.

The only way to paste into a textbox is Ctrl+V. Usually this is seen as a limitation but in this case is a blessing. Disabling Ctrl+V does the trick.

By the way, the reason I wanted to restrict them from pasting is security.

Thanks a ton once again!
 
Upvote 0
Tom,

Your method is definitely easier and better.

Mine just doesn't work because "Application" refers only the Excel front end. So it doesn't apply to a userform or even the VBA Editor.

Thanks a ton!
 
Upvote 0
You can use the KeyDown event to set the keycode value to 0 when the user attempts to Ctrl + v in a specified text box. Copy and paste this code into your userform then replace "TextBox1" with the name of the textbox you would like to prohibit pasting in.

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 86 And Shift = 2 Then
KeyCode = 0
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top