Kim M. said:
Thanks Albert. Actually I will not need this particular right-click menu
very often (just four text boxes on two forms). So if I don't want it
associated with the whole form, but only these particular text boxes,
where
would I paste this code?
Ok, I will suggest that it was just one form, then place the code in one
form.
However, y ou have 4 seperate places you need this.
Also, I am not 100% clear what code (option) is supposed to run when you
right click?
Also, is this a2007, or a previuous verson? In versions prior to 2007 you
can use the user interface (mouse) to build a right click custom menu and
not have to write any code at all.
However, in 2007 you have to use your macro apporach, OR you can use code as
we are doing.
I don't not know which of the above applies to your case. So, we just use
code here then....
I would suggest you place the code in a "standard code module" (call it
basMenus) or whatever you like.
The code would be:
Sub CreateRightClickMenu
Dim cB As CommandBar
Dim c As CommandBarControl
On Error Resume Next
Set cB = CommandBars.Add("TestC2", msoBarPopup, False, True)
If Err.Number = 0 Then
Set cB = CommandBars("Testc2")
CommandBars("Form Design").Controls("Hyperlink...").CopyFace
Set c = cB.Controls.Add(msoControlButton)
c.Caption = "Option1"
c.OnAction = "=msgbox('hello')"
c.PasteFace
End If
end sub
Then, in your startup form, go:
Call CreateRightClickMenu
Open up those two forms in design mode, and specify the shortcut menu for
the controls (you remove the macro setting you have for the 4 controls, and
simply place in the NAME of the right click menu we made. In the above, that
name used is Testc2
In the above, the onAction code is simply a msgbox "hello" command. It is
not 100% clear what your code does now. If the field your clicking on is a
regular text field with a file name that you want to launch, then change :
c.OnAction = "=msgbox('hello')"
to
c.OnAction = "=MyHyperLink()"
Then in your standard code module, we can have:
Public Function MyHyperLink()
Dim frmControl As Control
Set frmControl = Screen.ActiveControl
Application.FollowHyperlink frmControl.value
End Function
Note that the above code assume the text box on the form is a plane Jane
regular text box, NOT a hyperlink field on the form....