Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

click event for a toolbar button

Status
Not open for further replies.

gapel

Programmer
Mar 13, 2005
4
CA
In Word,I have a custom toolbar(commandbars).I put a custom controlButton in it , I choose
an icon picture(iconFace)and name(caption) it "Paco".

Here I add another controlButton and I copy the image of the first controlButton :

Sub NewBouton()
Set newB = CommandBars("Cast").Controls.Add(Type:=msoControlButton)

With newB
.Caption = "John"
.Style = msoButtonIconAndCaption
changeFaces ' Calling a Sub
.PasteFace ' Paste the first botton picture
.OnAction = "btnClic" ' Sub to go on click
End With


'This sub copy the picture from the first button
Sub changeFaces()
Set newControl = CommandBars.FindControl _
(Type:=msoControlButton, _
ID:=CommandBars("Cast").Controls("Paco").ID)
newControl.CopyFace
End Sub

Now I need to catch the click event. I want to know witch button was click and read is caption. example "John"

I start with something like this:

Sub btnClic(ByVal ctr As CommandBarButton)
Dim a As CommandBarButton
a = ctr
Dim b As String
b = a.Caption
End Sub

But it's more vb .net . I don't know how to put the Handle click_event

thank's

Gate

 
If there are two procedures assigned to two buttons, when any of them runs, then you know which one was clicked. You can also create Public variables when creating buttons to keep buttons.
It is also possible to get a handler from VBA to the button clicked. You need a class module with:

Public WithEvents btn As Office.CommandBarButton

Private Sub btn_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
' Ctrl is a handler to button here
End Sub

When you create two objects using the class and assign a specific buttons to each btn variable, you will get a button reference.
Search for Office library 'Click' event for details.

combo
 
I FIND MY OWN ANSWER!

Here the code. Any comment are welcome.

Note: make sure that: Click Tools and then References and select the reference for
"Microsoft Visual Basic for Applications Extensibility".


Sub AddButton()
Set newB = commandbars("Cast").Controls.Add(Type:=msoControlButton)

Dim newCast As String
Dim nb As String

newCast = "John" ' this will be use for the button caption and the Sub name

With newB
.Caption = newCast
.Style = msoButtonIconAndCaption
changeFaces
.PasteFace
.OnAction = newCast
End With

'Add a procedure for the click event of the controlButton
'Note: The click event ends up residing in the ThisDocument module.

nb = "Private Sub " & newCast & "()" & vbCrLf & _
" MsgBox ""It work!""" & vbCrLf & _
"End Sub"


ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString nb

Note: If you receive the following error:

"Programmatic Access to Visual Basic Project is not trusted."

Office XP and Office 2003 add a security option to deliberately lock out programmatic access to the VBA object model from any Automation client unless a user chooses to permit such access. This is a per user and per application setting, and denies access by default. To turn on the access, do the following:

1. Open the Office application in question. On the Tools menu, click
Macro, and then click Security to open the Macro Security dialog box.

2. On the Trusted Sources tab, click to select the Trust access to
Visual Basic Project check box to turn on access.

3. Click OK to apply the setting. You may need to restart the
application for the code to run properly if you automate from a
Component Object Model (COM) add-in or template.


Gate

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top