Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 29

Thread: right click context menu for textbox

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location

    right click context menu for textbox

    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.
    Last edited by JPG; 03-14-2020 at 04:47 PM. Reason: Sorry it should be textbox

  2. #2
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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.

  4. #4
    It works in all Word versions to date - see attached
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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?
    Last edited by JPG; 03-15-2020 at 05:01 AM.

  6. #6
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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.
    Last edited by JPG; 03-15-2020 at 05:14 AM.

  7. #7
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Example.docm
    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.

  8. #8
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    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
    Last edited by Artik; 03-15-2020 at 06:56 PM.

  9. #9
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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.

    Thank you for your assistance.

  10. #10
    Is there anything copied to the clipboard? You can't paste if the clipboard is empty.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    yes there certainly is. I copy text from the document each time I try this.

  12. #12
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    I will repeat what I said earlier:
    Quote Originally Posted by Artik View Post
    Apply the same patch in the EnableMenuItems procedure.
    Artik

  13. #13
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    I did apply it there also.
    Shall I post the .bas file ?

  14. #14
    Change both Dim statements for oControl to

    Dim oControl As Object
    See attached
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  15. #15
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Quote Originally Posted by gmayor View Post
    Change both Dim statements for oControl to (...) Object
    Why? After all, we will be dealing with MSForm.Control(s) all the time.

    Artik

  16. #16
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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.

  17. #17
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Can you show your example after all the changes?

    Artik

  18. #18
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    sure thanks for sticking with me.
    Attached Files Attached Files

  19. #19
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    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

  20. #20
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    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.?
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •