You can indeed hook into a custom commandbar button. The trick is declaring
the variable to the button using the WithEvents statement, and then you can
trap the Click event and run your custom code.
This code shows how to hook into or create a custom commandbar button when
Outlook starts, and illustrates the use of the button down and up states when
it is clicked:
Option Explicit
Dim WithEvents objLinkedCommandBarButton As Office.CommandBarButton
Private Sub Application_Startup()
CreateCustomButtonWithEventHook
End Sub
Sub CreateCustomButtonWithEventHook()
On Error Resume Next
Dim objCB As Office.CommandBar
Dim objCBB As Office.CommandBarButton
Set objCB = Application.ActiveExplorer.CommandBars("Standard")
'Retrieve a previously created custom commandbar button
Set objCBB = objCB.Controls.Item("My Custom Button")
If objCBB Is Nothing Then
'The commandbar button doesn't exist, create
Set objCBB = objCB.Controls.Add(MsoControlType.msoControlButton, , ,
, False)
With objCBB
.Caption = "My Custom Button"
.State = msoButtonUp
End With
End If
Set objLinkedCommandBarButton = objCBB
End Sub
Private Sub objLinkedCommandBarButton_Click(ByVal Ctrl As
Office.CommandBarButton, CancelDefault As Boolean)
If objLinkedCommandBarButton.State = msoButtonDown Then
objLinkedCommandBarButton.State = msoButtonUp
Else
objLinkedCommandBarButton.State = msoButtonDown
End If
MsgBox "I've been clicked!"
End Sub
--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
Try Picture Attachments Wizard for Outlook!
http://tinyurl.com/ckytm
Job:
http://www.imaginets.com
Blog:
http://blogs.officezealot.com/legault/
"Conan Kelly" wrote:
> Hello all,
>
> I would like to place a button on an existing toolbar that with one click
> will open my wife's .pst file and will show that button as pressed, and with
> the next click will close/remove her .pst file and show that button as
> unpressed.
>
> I found in the help file code to help me open and close the .pst file. I
> should be able to get that to work.
>
> The problem I'm having is that it appears the toolbars/buttons are not
> objects with events that I can write my code in.
>
> Is there anyway that I can put code in event procedures of buttons on
> toolbars?
>
> Thanks for any help anyone can provide,
>
> Conan Kelly
>
>
>