Problem in permanent type add-in toolbar

P

paresh

Hi, I have been facing a issue with dealing with my add-in toolbar. I want to
make my toolbar permanent so that use can hide and reposition it
permanently(even after restarting Outlook). At the same time, I want to
support my add-in toolbar for all opened Outlook explorer.

I want to make sure that at any point of time there must be one and only one
toolbar on any Outlook window. I have tried below methods but none worked:

1. Tried deleting toolbar on shutdown. This doesn't delete the toolbar and
each restarting outlook adds new toolbar.

Private Sub AddinInstance_OnBeginShutdown(custom() As Variant)
MyCommandBar.Delete
End Sub

Private Sub AddinInstance_Terminate()
MyCommandBar.Delete
End Sub

2. I tried deleting toolbar on Explorer close event but didn't work.
Whenever I open Outlook folder by right clicking it and selecting "Open in
new window", it keep adding new toolbar but does't delete.

Private Sub myExpl_Close()
MyCommandBar.Delete
If out_appt.Explorers.Count < 1 Then
Set myExpl = Nothing
Set myColExpl = Nothing
End If
End Sub

3. Tried checking existence of toolbar but this will not add toolbar when I
right click on outlook folder and open in new window.

Set MyCommandBar = myExpl.CommandBars.Item(TOOLBARNAME)
If Not MyCommandBar Is Nothing Then
MsgBox "exists"
Exit Sub
End If

Could anyone please help me here. I think if I can do any of below then I
would all set here:
1. Delete existing toolbar while closing explorer event
2. Check the existing of toolbar explorer wise. Though I am setting
myExpl to currently active explorer, my code always returns true for
"myExpl.CommandBars.Item(TOOLBARNAME)" whenever I open outlook folder in new
window.

Thanks.

=== MY CODE ===

Option Explicit
Public out_appt As Outlook.Application
Public WithEvents MyButton As Office.CommandBarButton
Public MyCommandBar As Office.CommandBar
Public WithEvents myColExpl As Outlook.Explorers
Public WithEvents myExpl As Outlook.Explorer
Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
Set out_appt = Application
End Sub
Private Sub AddinInstance_OnStartupComplete(custom() As Variant)
Set myColExpl = out_appt.Explorers
If out_appt.Explorers.Count > 0 Then
Call CreateToolBar
End If
Exit Sub
End Sub
Private Sub myColExpl_NewExplorer(ByVal Explorer As Outlook.Explorer)
If myExpl Is Nothing Then
Set myExpl = Explorer
End If
If out_appt.Explorers.Count > 0 Then
Call CreateToolBar
End If
End Sub
Private Sub myExpl_Close()
MyCommandBar.Delete
If out_appt.Explorers.Count < 1 Then
Set myExpl = Nothing
Set myColExpl = Nothing
End If
End Sub
Private Sub MyButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
On Error Resume Next
MsgBox "button clicked"
End Sub
Private Sub CreateToolBar()
If out_appt.Explorers.Count = 0 Then
Exit Sub
End If
Set myExpl = out_appt.ActiveExplorer
Const TOOLBARNAME = "My Toolbar"

' Set MyCommandBar = myExpl.CommandBars.Item(TOOLBARNAME)
' If Not MyCommandBar Is Nothing Then
' MsgBox "exists"
' Exit Sub
' End If
'
Set MyCommandBar = myExpl.CommandBars.Add(TOOLBARNAME, msoBarTop, False,
False)
Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "890", ,
False)
With MyButton
.Caption = "&Foo Button"
.Enabled = True
.OnAction = "!<PermToolbarTesting.Connect>"
.Tag = "890"
.FaceId = 362
.Style = 3
.Visible = True
End With
MyCommandBar.Visible = True
End Sub
 
K

Ken Slovak - [MVP - Outlook]

I'll answer this one. Don't make your UI permanent. That would leave it
there even if your addin isn't running or even if it's uninstalled. Always
add any UI you add as temporary.

You should be using different Explorer objects for each open Explorer in the
Explorers collection. You add an Explorer class wrapper to a collection when
a new Explorer is opened and remove it when it is closed. You do that in
Explorers.NewExplorer(). The wrapper class should declare any Explorer
events you intend to handle as well as your CommandBarButton events and
declarations for your UI and any folder events.

When you add an Explorer to your wrapper collection you then add the UI in
the first Explorer.Activate() event. For an initial Explorer you bypass
that. You hold a key value that's an index into the collection for each open
Explorer. You add that to a Tag value to get a unique Tag value for each
menu/toolbar/button. That way you can identify each one and get unique
clicks for each.

You can download a template project in VB6 that shows how to work with
wrapper classes like that from
http://www.slovaktech.com/outlook_2007_templates.htm, it is set up for
Outlook 2007 use with VB6.
 
P

paresh

Thanks Ken but I think it is very complected way for me till I am acquainted
with it. I am also not sure if you distinguished all explorer using objects
then you can share the values you have loaded during startup.

I have came out with the very simple code that meets my all requirements.
Could you please tell me if there is anything wrong? It creates the toolbar
on new explorer event for any new Outlook window and seem to be working fine.

=== MY CODE ====

Option Explicit
Public out_App As Object
Public WithEvents MyButton As Office.CommandBarButton
Public MyCommandBar As Office.CommandBar
Public WithEvents myColExpl As Outlook.Explorers
Public WithEvents myExpl As Outlook.Explorer

Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal
ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As
Object, custom() As Variant)
Set out_App = Application
End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
Set MyButton = Nothing
Set MyCommandBar = Nothing
End Sub

Private Sub AddinInstance_OnStartupComplete(custom() As Variant)
Set myColExpl = out_App.Explorers
If out_App.Explorers.Count > 0 Then
Call CreateToolBar
End If
End Sub

Private Sub myColExpl_NewExplorer(ByVal Explorer As Outlook.Explorer)
If myExpl Is Nothing Then
Set myExpl = Explorer
End If

If out_App.Explorers.Count > 0 Then
Call CreateToolBar
End If
End Sub

Private Sub myExpl_Close()
If out_App.Explorers.Count < 1 Then
Set myExpl = Nothing
Set myColExpl = Nothing
End If
End Sub

Private Sub MyButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "button clicked"
End Sub

Private Sub CreateToolBar()
Dim testIt As Boolean
If out_App.Explorers.Count = 0 Then
Exit Sub
End If

Const TOOLBARNAME = "Permanent Toolbar Testing2"
On Error Resume Next
testIt = Not out_App.ActiveExplorer.CommandBars(TOOLBARNAME) Is Nothing
If testIt Then
Set MyCommandBar =
Outlook.Application.ActiveExplorer.CommandBars.Item(TOOLBARNAME)
Else
Set MyCommandBar =
Outlook.Application.ActiveExplorer.CommandBars.Add(TOOLBARNAME, msoBarTop,
False, False)
End If

Set MyButton = MyCommandBar.Controls.Add(msoControlButton, , "891", ,
True)
With MyButton
.BeginGroup = True
.Caption = "My Permanent Button"
.Enabled = True
.OnAction = "!<PermToolbarTesting2.Connect>"
.Tag = "891"
.FaceId = 362
.Style = 3
.Visible = True
End With
MyCommandBar.Visible = True
End Sub

Thanks,
Paresh
 
K

Ken Slovak - [MVP - Outlook]

If the code you have now meets your requirements and seems to work there's
no need for me to review it.
 
P

paresh

Ken, actually I am just wondering why we have to write the very complex code
to handle the toolbar for all opened Outlook windows individually? I
understood your concept of generating TAG id uniquely after creating the
explorer object but I haven't noticed multiple events firing when I have two
Outlook window open and both have same toolbar "My Toolbar" with same Buttons
and same TAGs.

In short, could you give me a simple example where we have to use the
concept given by you and my code will not work? I am intermediate level in
add-in so I might not be aware of many things.

Thanks,
Paresh
 
K

Ken Slovak - [MVP - Outlook]

Here's one example.

You have a toggle button that indicates a state for doing something. If you
have 2 Inspectors open and both use the same Tag value for a
CommandBarButton, both will get the click event that toggles the button. So
toggling in one toggles both. Then when some action is taken based on the
button state you can't maintain separate states for the button in each
Inspector.

Using wrapper classes solves a number of problems such as that with unique
Tag values, individually handling events in multiple open windows, handling
discrete ribbon clicks that are directed to only one Inspector where you
pass the click to a handler in your wrapper class, etc.

Every advanced Outlook developer I know uses wrapper classes and
collections, but your mileage may vary. I'd never do an Outlook addin myself
without wrapper classes.
 
P

paresh

Thanks that makes perfect sense to me but I think I don't need to write
wrapper class for explorer as my requirements are very simple and nothing is
shared between the toolbar clicks that might cause the issue.

What all I want is to put toolbar whenever new explorer open and perform the
operation when button click.

Thanks a lot for your help.
Paresh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top