PDA

View Full Version : [SOLVED:] right click context menu for textbox



JPG
03-14-2020, 04:40 PM
Anyone have some code that actually works to give a right click context menu on a textbox. I just want to be able to paste to the box with the mouse.
I have tried some code from this site but without success.

gmayor
03-14-2020, 10:09 PM
This works -https://wordmvp.com/FAQs/Userforms/AddRightClickMenu.htm

JPG
03-15-2020, 12:27 AM
I tried this one and it threw up an error.

Run-time error '13': Type mismatch

Debug took me to this statement


' Set object reference to the textboxthat was clicked
Set oControl = oForm.ActiveControl



It notes on the page " It has been tested in Word 97 and Word 2000 under Windows 98 and 2000."

I am on Word 2010, so something needs to be fixed in that .bas file I guess.

gmayor
03-15-2020, 02:51 AM
It works in all Word versions to date - see attached

JPG
03-15-2020, 03:30 AM
your example works correctly but not in my form


I just made my own test example on another form and it works fine.


So what could be wrong with the other one?

JPG
03-15-2020, 04:39 AM
I found the difference.
My form has a Mulitpage element with the textbox on that.
Try your example with that setup, if you would please and see if you get the error.


UPDATE

I change the code in the mocPopupMenu

Dim oControl As MSForms.MultiPage

Now it is working.... EXCEPT paste... I just can't believe this.

JPG
03-15-2020, 09:47 AM
26159
here is the sample but with the box on a multipage.
If you are able to get it to paste with the menu it would be fantastic.

Artik
03-15-2020, 06:44 PM
Change the content of procedure ShowPopup a little
Public Sub ShowPopup(oForm As UserForm, strCaption As String, X As Single, Y As Single)

Dim oControl As MSForms.Control

Static click_flag As Long

' The following is required because the MouseDown event
' fires twice when right-clicked !!
click_flag = click_flag + 1

' Do nothing on first firing of MouseDown event
If (click_flag Mod 2 <> 0) Then Exit Sub

' Set object reference to the textboxthat was clicked
Set oControl = oForm.ActiveControl

If TypeName(oControl) = "MultiPage" Then
Set oControl = oControl.Pages(oControl.Value).ActiveControl
End If

' If click is outside the textbox, do nothing
If X > oControl.Width Or Y > oControl.Height Or X < 0 Or Y < 0 Then Exit Sub
(...)
Apply the same patch in the EnableMenuItems procedure.

Artik

JPG
03-16-2020, 01:52 AM
I am not getting it to work on my form. Paste is greyed out. So could it be the code that is checking the clipboard is also wrong now.? Please if possible can you do the fix in the .docm example I uploaded and check it works, then upload the working .docm
I have gone in circles with this for too long. :banghead:

Thank you for your assistance.

gmayor
03-16-2020, 03:07 AM
Is there anything copied to the clipboard? You can't paste if the clipboard is empty.

JPG
03-16-2020, 03:16 AM
yes there certainly is. I copy text from the document each time I try this.

Artik
03-16-2020, 05:49 AM
I will repeat what I said earlier:

Apply the same patch in the EnableMenuItems procedure.
Artik

JPG
03-16-2020, 05:56 AM
I did apply it there also.
Shall I post the .bas file ?

gmayor
03-16-2020, 07:08 AM
Change both Dim statements for oControl to


Dim oControl As Object

See attached

Artik
03-16-2020, 07:18 AM
Change both Dim statements for oControl to (...) Object Why? After all, we will be dealing with MSForm.Control(s) all the time.

Artik

JPG
03-16-2020, 07:20 AM
Thanks, I can see that works and so the only other difference is that I have a frame in the MultiPage and it is this that is now causing it not to work. When I move the textbox outside the frame it works. So is there a fix for that setup. Sorry for all this but but. There was no mention of limitations for this .bas file. It appeared generic.

Artik
03-16-2020, 07:24 AM
Can you show your example after all the changes?

Artik

JPG
03-16-2020, 07:40 AM
sure thanks for sticking with me.

Artik
03-16-2020, 10:23 AM
A few words of explanation.
In the original code, the author assumed that TextBox will be in a container that is UserForm
Set oControl = oForm.ActiveControl But UserForm is not the only possible container. The UserForm container may also contain a MultiPage container and a Frame container. At your place, TextBox2 is directly in the UserForm container. In contrast, TextBox1 is strongly nested. It's in the Frame container, and this one is in the Multipage container that is in the UserForm container. To find out which control is active, you must go through all the containers.
The corrected code, in addition to placing the nested control in Frame/MultiPage/UserForm, also provides for other nesting: MultiPage/Frame/UserForm. However, it does NOT provide e.g. Frame/Frame/UserForm etc.
In both procedures, improve on this passage:
Set oControl = oForm.ActiveControl


If TypeName(oControl) = "MultiPage" Then
Set oControl = oControl.Pages(oControl.value).ActiveControl
If TypeName(oControl) = "Frame" Then
Set oControl = oControl.ActiveControl
End If
ElseIf TypeName(oControl) = "Frame" Then
Set oControl = oControl.ActiveControl
If TypeName(oControl) = "MultiPage" Then
Set oControl = oControl.Pages(oControl.value).ActiveControl
End If
End If


In my opinion, the declaration of the oControl variable may point to the type MSForm.Control.
Dim oControl As MSForms.Control


Artik

JPG
03-16-2020, 12:46 PM
It is in my case worse.
I have 5 pages and it so happens that on page 3 I have nested 2 frames as per the modified docm
It certainly does work for me on the other pages where I only have one frame
How can this be overcome.?

Artik
03-16-2020, 04:02 PM
We need to slightly change the approach to the problem. Instead of looking for which control is active, explicitly point it and pass it as an argument to ShowPopup and EnableMenuItems.


Procedures that have changed:
ShowPopup
EnableMenuItems
TextBox1_MouseDown
TextBox2_MouseDown
TextBox3_MouseDown

Artik

JPG
03-16-2020, 04:38 PM
Thanks I will take a look tomorrow.

JPG
03-17-2020, 01:38 AM
This is excellent work I am sure. Works perfectly in any place I have the code specific for that box.
Thank you for your time and effort, knowledge on this. It really had frustrated me that ones were saying it worked but I could not get it to work. I had no idea initially that frames would be of any issue to the other code. Hopefully your code will get a good distribution. I think it should be renamed to set it apart from the other module. I will put a link in mine to this post.

JPG
03-31-2020, 04:03 PM
I just shared this macro with one using 64bit Word and it objects.
PtrSafe attribute required.
Where and how do I do this?

Artik
03-31-2020, 04:51 PM
Sorry, I will not convert to 64-bit version. I feel too weak.

Artik

JPG
03-31-2020, 05:04 PM
Sorry about that.

Can anyone else help please.?

JPG
04-01-2020, 01:41 AM
Can someone with a 64bit Word try this file to see if it loads without objections.

Artik
04-01-2020, 02:36 AM
This is unlikely to work. The 64-bit conversion is not a simple conversion of Long to LongPtr. Part of the API parameter declarations are still Long. But some of the variable declarations used in the procedures below must also change.
Read:
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/ee691831(v=office.14)?redirectedfrom=MSDN
and download the Win32API_PtrSafe.TXT file.

You must also use conditional compilation to get a file that will work on both 32-bit and 64-bit versions.

Artik

JPG
04-01-2020, 02:51 AM
Thanks I had looked at that page and the best I could do was what I posted. Someone with the knowledge and time can surely make this work on 32 and 64 and we will all benefit from being able to have right click menu in any place on VBA Forms. I am ok as I have 32 bit, but I would like 64 bit users to be able to use it so they can use my macro form with menu.