Adding a right-click context menu to Excel VBA TextBoxes

R

ram gopal

Hello All,

I am created text boxes using
views-->Toolbars---->control toolbox in excel.

I need VBA code for right mouse button to copy, paste,delete and select
all options.

Could some one help me above situation.

Thanks,
Ram


*** Sent via Developersdex http://www.developersdex.com ***
 
D

Doug Glancy

Ram

Add this code into the code module for the worksheet that contains the
textbox. To do this, right-click the tab of the worksheet with the textbox
and choose "View Code" and then paste the code into the module. Change the
name of the textbox to match yours:

Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
If Button = vbKeyRButton Then
Call MakeMenu
Application.CommandBars("TextBox Bar").ShowPopup
End If
End Sub

Sub MakeMenu()
Dim cbTextBox As CommandBar
Dim cmdTest As CommandBarButton

'in case it already exists
Call DeleteBar
Set cbTextBox = CommandBars.Add(Name:="TextBox Bar", Position:=msoBarPopup,
temporary:=True)
With cbTextBox
Set cmdTest = .Controls.Add(Type:=msoControlButton, temporary:=True)
With cmdTest
.Style = msoButtonIconAndCaption
.FaceId = 3
.Caption = "test"
.OnAction = "TestRoutine"
End With
End With
End Sub

Sub DeleteBar()
'in case it doesn't exist
On Error Resume Next
CommandBars("TextBox Bar").Delete
End Sub

Then in a regular module, add this code. This is the subroutine that's
named in the OnAction property of the button. This is just an example:

Public Sub TestRoutine()
MsgBox "Test Pressed!"
End Sub

hth,

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top