Building a context menu for a userform Textbox1 in a Word doc for 64 bit system

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
582
I've used the following for to show context menus in any userform textbox in Excel VBA and it has worked very well:
Code:
Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
       If Button = 2 Then
        Set g_txtActiveTextbox = TextBox1
        BuildTextboxMenu X, Y
    End If
End Sub

Sub BuildTextboxMenu(X As Single, Y As Single)
    ' Remove any existing reference
    On Error Resume Next
    CommandBars("MyTextboxMenu").Delete
    On Error GoTo 0
        With CommandBars.Add(Name:="MyTextboxMenu", Position:=msoBarPopup)
        With .Controls.Add(Type:=msoControlButton)
            .OnAction = "Textbox_Cut"
            .Caption = "Cu&t"
        End With
        With .Controls.Add(Type:=msoControlButton)
            .OnAction = "Textbox_Copy"
            .Caption = "&Copy"
        End With
        With .Controls.Add(Type:=msoControlButton)
            .OnAction = "Textbox_Paste"
            .Caption = "&Paste"
        End With
        With .Controls.Add(Type:=msoControlButton)
            .OnAction = "Textbox_Clear"
            .Caption = "Cle&ar"
        End With
        With .Controls.Add(Type:=msoControlButton)
            .OnAction = "Textbox_Select"
            .Caption = "Select A&ll"
            .BeginGroup = True
        End With
        .ShowPopup
    End With
 
    ' remove it
    CommandBars("MyTextboxMenu").Delete
End Sub
However - when I copied this code and pasted into a Word VBA userform doc I get this:
Capture.PNG
Capture.PNG


Macro security settings are set to "enable all macros" in the Trust Settings in Word Options
What is this message telling me, either about the macro not being found or an issue with the security settings ?

Thanks for anyone's help.
cr
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Dan_W

Active Member
Joined
Jul 11, 2018
Messages
258
Office Version
  1. 365
Platform
  1. Windows
I suspect it may be your settings. My reason for thinking that is that I tried your code, and it works - except for one problem which may just be in the extract above.

When I ran that code, it came up with exactly the same message as you have in the image you uploaded but it also identified the macro that it was trying to run (but couldn't find). Your code above doesn't include the corresponding subroutines that run when clicked (as set with the .OnAction property). It could not, for example, find the sub for Textbox_Cut (obviously). I made some dummy subs, stored them in the standard modules, and it worked fine.

To confirm - I checked this in Word 64bit.
 

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
582
Hi Dan - thanks for the reply and helping me with this. I'm trying to understand exactly what you are saying in your reply.
Are you saying you copied and pasted this code in a userform with a Textbox1 in Word VBA and when you right click in Textbox1 the copy paste context menu comes up and allows you to copy a highlighted selection of text and paste it say, to any other Textbox2?

Or, are you saying that when you right clicked in Textbox1, the context menu appeared, but when you selected say, copy, it generated this same error message for you ?

When you said you made dummy subs, what code did they include that caused it to work that is not present in my code ?

Sorry if I sound too elementary in my questions - just want to understand what you did(added, changed in the code) that made it work for you. The only other thing I can add is that the code is stored in the userform Textbox1 MouseUp location and not in a standard module. CODEBLOCK.PNG
CODEBLOCK.PNG


Also, you said, it may be in the settings on this laptop. I rechecked the settings in the Ttust Settings to enable all macros. Are there any other settings if any, that need to be changed in the sidebar panel on the left that would make it run ? Once again, thanks for all your help. Frustrating, because this code works great in an Excel VBA userform Textbox, as it has for years. Sorry again for the lengthy reply back. cr.
TRUSTCENTERSETTINGS.PNG
 

Dan_W

Active Member
Joined
Jul 11, 2018
Messages
258
Office Version
  1. 365
Platform
  1. Windows
Hi

What I'm saying is:
  • I copied your code, and put it in a Userform in Word (64 bit).
  • I added a textbox, named textbox1 - as your code requires.
  • I ran it, and right clicked in the textbox. The menu displays fine - showing the options: cut, copy, paste and select all.
  • When I click on one of the options, it throws up an error.

error.jpg


  • As you can see, it is almost exactly like yours, except for the first sentence which identifies the macro that cannot be run.
  • In your code, you will see that you have written .OnAction = "Textbox_Cut". This tells VBA that when the user select this menu item, VBA should find and execute a subroutine that is called "Textbox_Cut".
  • Perhaps you have that subroutine in the rest of your code, perhaps not. In any event, I didn't, so I instead made a dummy routine called "Textbox_Cut" that just displayed a messagebox saying "It worked" when I selected the "Cut" option.
So, the reason I got this error message is because I don't have on my computer the code that you have on yours, and it wasn't included in your excerpt above. But that's beside the point. I remedied that, and it works fine now. What is important is that your error message is not the same as mine - which leads me to suspect (though I could be wrong) that your issue relates to the security settings.

In terms of your security settings, they look just like mine. This is a good guide on what to do with macro settings, but to be frank, I always seem to have all manner of problems everytime I have to set up a new laptop - I still occasionally encounter this issue with my work laptop. The only other thing I can suggest is you may want to set your working directory as being a trusted location, if it isn't already (second menu item from the top in your screen grab). Good luck - Let me know how it goes.
 

Dan_W

Active Member
Joined
Jul 11, 2018
Messages
258
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Sorry - forgot to add that I had to put the textbox_cut code in a standard module in order to work. Your code, however, went into a user form.

I only just saw your point about the code otherwise working for you in Excel - that's odd. If your settings are the same in both applications, it should work. My immediate is that perhaps you didn't copy all of the relevant code across? What happens if you export the userform from Word / Excel and then import it into the other application?

And it just dawned on me now - a quick way of working out of the source of the problem - do other macros work in Word? Or is it just this one?
 

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
582
These 3 QAT buttons are macros( UP.Show, THEEAST.Show, Userform1.Show) that open the 3 userforms shown. Usable on any new blank doc since i set it for All documents

QATBTNS.PNG

FORMS.PNG

I did not copy any .OnAction "Textbox_Cut, Copy, Paste, etc."code over to Word. Just what I showed you. That may be the problem. I just need to find where those Subs are located because to me, if this code runs all day on every textbox in Excel, then it must be drawing the subs codes from some source location in Excel. Plan 2: create new cut, copy, paste macros in Word by recording steps renaming to existing names and see if that works. Many thanks again for all your help. cr
 

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
582
Sorry - forgot to add that I had to put the textbox_cut code in a standard module in order to work. Your code, however, went into a user form.

I only just saw your point about the code otherwise working for you in Excel - that's odd. If your settings are the same in both applications, it should work. My immediate is that perhaps you didn't copy all of the relevant code across? What happens if you export the userform from Word / Excel and then import it into the other application?

And it just dawned on me now - a quick way of working out of the source of the problem - do other macros work in Word? Or is it just this one?
Hi Dan - found the problem! - you were 100% correct - I went though all of the modules in my Excel app and found this in Module5:
Code:
Option Explicit
Public g_txtActiveTextbox As MSForms.TextBox

Public Sub ShowDemo()
    UserForm1.Show
End Sub

Public Sub Textbox_Clear()
    g_txtActiveTextbox.Text = ""
End Sub
Public Sub Textbox_Select()
    g_txtActiveTextbox.SelStart = 0
    g_txtActiveTextbox.SelLength = Len(g_txtActiveTextbox.Text)
End Sub
Public Sub Textbox_Paste()
    g_txtActiveTextbox.Paste
End Sub
Public Sub Textbox_Copy()
    g_txtActiveTextbox.Copy
End Sub
Public Sub Textbox_Cut()
    g_txtActiveTextbox.Cut
End Sub
Copied and pasted in Word module and it now works as it should. Simple but missed on my part.
Thanks again for all your help.
cr
 

Dan_W

Active Member
Joined
Jul 11, 2018
Messages
258
Office Version
  1. 365
Platform
  1. Windows
No problem. Glad it's working now.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,713
Messages
5,573,761
Members
412,550
Latest member
soking
Top