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
917
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,663
Messages
6,173,649
Members
452,525
Latest member
DPOLKADOT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top