Outlook Add-ins Button?

H

he

Hello,

Outlook 2003,
IDE: MS VS 2005,
Prg.Language: VB.Net

I've created an Outlook Add-In with VS 2005 which adds a button to a custom
command bar in the mailitem inspector. I'm using the following code:

Dim WithEvents btnZeitStempel As CommandBarButton
Dim WithEvents myOLInspectors As Outlook.Inspectors

Public Sub OnConnection(.....)
applicationObject = application
addInInstance = addInInst
Dim oapps As Outlook.Application = CType(applicationObject,
Outlook.Application)
myOLInspectors = oapps.Inspectors
......' Call OnStartupComplete...... vs.
End Sub

Private Sub myOLInspectors_NewInspector(ByVal Inspector As
Microsoft.Office.Interop.Outlook.Inspector) Handles
myOLInspectors.NewInspector

Dim objMailItem As MailItem = CType(Inspector.CurrentItem, MailItem)
Select Case objMailItem.MessageClass
Case "IPM.Note"
if btnShowChecked = True
ShowZeitStempel(Inspector, True) 'Show Button
Else
ShowZeitStempel(Inspector, False) 'Delete only Button
End If
End Select
End Sub

Private Sub ShowZeitStempel(ByVal Inspector As
Microsoft.Office.Interop.Outlook.Inspector, ByVal show As Boolean)

Dim commandBar As CommandBar = Nothing

If show Then
If Toolbars.IsCommandBarExist(Inspector, "fat") = False Then
If commandBar Is Nothing Then
commandBar = Inspector.CommandBars.Add("fat",
MsoBarPosition.msoBarTop, False)
btnZeitStempel =
Toolbars.CreateCommandBarButton(commandBar)
commandBar.Visible = True
End If
Else
commandBar = Inspector.CommandBars("fat")
If Toolbars.IsCommandBarButtonExist(commandBar, "btn") Then
commandBar.Controls("btn").Delete()
btnZeitStempel =
Toolbars.CreateCommandBarButton(commandBar)
commandBar.Visible = True
Else
btnZeitStempel =
Toolbars.CreateCommandBarButton(commandBar, Inspector)
commandBar.Visible = True
End If
End If

Else '---->Delete Only Button and show only my "fat" CommandBar
......
..........
end if
End Sub

Private Sub btnZeitStempel_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
Handles btnZeitStempel.Click
MsgBox("Button Clicked...")
End Sub
-----------------------

The command bar itself is created with flag 'temporary = false'.
Right after creation the button does work perfectly. The second or third
time I open up Outlook the button is still there but does not work at all, I
can click it without any code being executed, the button forgets the function
to call we previously assigned (see code above).

I've tried deleting and recreating the button on the command bar. When
opening an inspector all command bars (default and my custom bar) are present
and can be enumerated and accessed in my code, but on my custom command bar
there is no button available at this time (controls.count = 0). Therefore I
can't delete and recreate the button. When the inspector window finally is
displayed, my button is visible, though controls.count was 0 a few moments
ago. And it's not working when clicked.

Any ideas on how to create a command bar button that doesn't loses it's
functionality after closing Outlook?

Thanks, Regards
 
K

Ken Slovak - [MVP - Outlook]

You should not use the NewInspector event handler to add UI to an Inspector.
You get a weak object reference in that event handler that doesn't guarantee
that all properties of the Inspector are valid. You should use NewInspector
to check only for the MessageClass or Class of an item in the new Inspector
and add your UI in the first Inspector.Activate event that fires after that.

In general the best practice is to create the button each time a new
Inspector is added to the Inspectors collection, using the Temporary setting
and then delete it when the Inspector is closing. You assign the object
variable for your button at the class or module level so it doesn't get
garbage collected unexpectedly and remains in scope as long as the Inspector
is still alive.
 
M

matzer

Ken Slovak - said:
You should not use the NewInspector event handler to add UI to an Inspector.
You get a weak object reference in that event handler that doesn't guarantee
that all properties of the Inspector are valid. You should use NewInspector
to check only for the MessageClass or Class of an item in the new Inspector
and add your UI in the first Inspector.Activate event that fires after that.

In general the best practice is to create the button each time a new
Inspector is added to the Inspectors collection, using the Temporary setting
and then delete it when the Inspector is closing. You assign the object
variable for your button at the class or module level so it doesn't get
garbage collected unexpectedly and remains in scope as long as the Inspector
is still alive.

Hi Ken,

thank you. Now we are using the following code to access an inspector:

Public Class ThisAddIn
Dim WithEvents objOLInspectors As Inspectors
Dim WithEvents objItemInspector As Inspector

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
objOLInspectors = Me.Application.Inspectors
End Sub

Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Shutdown
objItemInspector = Nothing
objOLInspectors = Nothing
End Sub

Private Sub objOLInspectors_NewInspector(ByVal Inspector As
Microsoft.Office.Interop.Outlook.Inspector) Handles
objOLInspectors.NewInspector
objItemInspector = Inspector
End Sub

Private Sub objItemInspector_Activate() Handles objItemInspector.Activate
MsgBox("active...")
Dim objToolbar As CommandBar
objToolbar = objItemInspector.CommandBars("OLTools")
MsgBox(IsNothing(objToolbar))

If IsNothing(objToolbar) Then
objToolbar = objItemInspector.CommandBars.Add("OLTools",
MsoBarPosition.msoBarTop, False, False)
objToolbar.Visible = True
End If
End Sub
End Class

Problem now is that the code seems to stop executing after
"MsgBox("active...")". No error is displayed but
"MsgBox(IsNothing(objToolbar))" is not fired, when running the add-on project.
 
K

Ken Slovak - [MVP - Outlook]

Ah, OK, so you're using VSTO.

First of all, by displaying a MessageBox during the Activate event you
immediately fire the event again as soon as you acknowledge the MessageBox.
You're setting up an endless loop that way.

Second, you will get an exception if you try to instantiate a CommandBar
object variable if the CommandBar doesn't exist. So you need some error
handling.

Try something like this in the Activate event handler:

Dim objToolbar As Office.CommandBar
Try
objToolbar = objItemInspector.CommandBars.Item("OLTools")
Catch ex As Exception
Debug.WriteLine(ex.Message)
objToolbar = Nothing
End Try

'MsgBox(IsNothing(objToolbar))

If IsNothing(objToolbar) Then
objToolbar = objItemInspector.CommandBars.Add("OLTools", _
Office.MsoBarPosition.msoBarTop, False, False)

objToolbar.Visible = True
End If

You still should test the MessageClass or Class of the Inspector.CurrentItem
in NewInspector to only handle Inspectors you want, and you probably should
use an Inspector wrapper if you intend to handle cases where more than one
Inspector is opened at a time. I have an Outlook 2007 VSTO 2005 SE VB.NET
project on my Web site that shows using Inspector and Explorer wrappers, but
it's Outlook 2007 only. You can find Outlook 2003 Inspector wrappers on
www.outlookcode.com.
 

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