A button on a toolbar to open a .pst file

  • Thread starter Thread starter Conan Kelly
  • Start date Start date
C

Conan Kelly

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
 
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,

I want to thank you again for the info. I haven't tried yet to get this to
do what I want it to do. But I did paste your code in "as-is" and like how
it works. I'm pretty sure I can get this to do what I want it to.

Usually when I post questions and they don't get answered in a day or two, I
know that they will never get answered. That is what I was expecting with
this question. It seems like no one else wanted to touch this one. But you
came through.

Thanks again,

Conan
 
Back
Top