PDA

View Full Version : Solved:



JamJam11
02-13-2011, 03:48 PM
Hi everyone,
This is my first post here! :hi:

I've created a UserForm in Word (I realise this is an Excel thread) and want to add a right click Copy/Paste option that the user can use to copy or paste content in text boxes on the form.

I've searched everywhere for the code to do this and the information on this thread (though for Excel) is the closest I've come to getting something to work.

But...

Can someone help me?

I've added the following code to 'module1':

Sub MakePopUp()
'Remove any old instance of MyPopUp
On Error Resume Next
CommandBars("MyPopUp").Delete
On Error GoTo 0

With CommandBars.Add(Name:="MyPopUp", Position:=msoBarPopup)
.Controls.Add Type:=msoControlButton, ID:=19
.Controls.Add Type:=msoControlButton, ID:=22
End With
End Sub

And this code to the UserForm1:

Private Sub txtReqDate_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then
Application.CommandBars("MyPopUp").ShowPopup
End If
End Sub

In addition to this I added to a Sub called Private Sub UserForm_Initialize() the line:

Call MakePopUp

When I load the UserForm and right click In the text box called 'txtReqDate' I do get a menu popup with 'Copy' and 'Paste' as options but selecting one of the options doesn't do anything i.e. it doesn't copy or paste any text.

I don't receive any error messages.

I've obviously missed something important. Can anyone help me? I've been researching this for days now.

Thanks,
John

mdmackillop
02-13-2011, 06:27 PM
You need to add a Click macro for each button specifying what is to be copied and location for pasting.

Bob Phillips
02-14-2011, 02:08 AM
I think he is trying to use the built-in copy/paste Malcolm.

JamJam11
02-14-2011, 02:38 AM
@xld
That's correct. As you know with userforms you can't just right click in a field and receive the context menu (copy, paste etc) and I need to provide this as an option for my users.

So I'm trying to get this code working to emulate a context menu and provide the functionality. The menu currently appears but the copy/paste functions aren't executing.

The user needs to be able to right click in any of the text boxes in the form and copy/paste.

I'm someone who 'gets by' when it comes to VBA, but have written code in Excel, Word and Access. But this one has me stumped.
:(
Thanks,
John

GTO
02-14-2011, 03:47 AM
Here is a nice example by Andy Pope:

http://ajpi.info/vba.htm

JamJam11
02-14-2011, 04:16 AM
@GTO

This is really freaky... I've just come across the very same page while Googling this problem!

Thank you though for posting it here. I've adapted Andy Pope's code to work with my Word UserForm and it works!

The only slight downside is the file size of my document has increased more than I wanted it to because of the amount of VBA needed to get the context menus working.

I guess you can't have everything. But at last my problem is solved!

Many thanks everyone for your help.
:)
John