Addin button not showing up -- problem with ActiveExplorer?

  • Thread starter Thread starter steven.shannon
  • Start date Start date
S

steven.shannon

Hey all,

Apologies for the ensuing long-winded post, but I want to be as clear
as I can about what's going on.

I have a COM addin that was developed for Outlook 2002/XP and later.
It began in VBA but was eventually wholly migrated to .NET in order to
incorporate XML web services which are an integral part of it. It's
supposed to create a new button in the standard Outlook toolbar that
runs the program (which acts like a standard wizard tool). Most
clients report no problem with the install, but a few have reported
that the button doesn't show up when they install the addin, and there
doesn't seem to be any consistency as to what versions of Windows or
Outlook they are running. (i.e. the issue has occurred on a Windows
2000 machine as well as XP). The frustrating part is that, up until
recently, I've been completely unable to replicate the issue in-house.

However, I recently dug out an old laptop running Windows 2000 and
Outlook 2002, and after attempting to install the addin I finally see
what the client sees. The addin does in fact get added to the COM Add-
ins list in Outlook, and the box is checked, but the button does not
appear in the toolbar. I've checked the registry, and the correct
entries are added into HKCU and HKCR. Comparing the registry settings
on another machine where the addin installs just fine, they are
identical from what I can tell. If I uncheck and re-check the box in
the addins list, a runtime error occurs and the LoadBehavior key in
the registry switches from 3 to 2.

After compiling a few different test versions of the addin with print
statements in various places, I've determined that the OnConnection
event is firing successfully. Also, the OnStartupComplete event will
fire with a print statement there and nothing else. However, the
moment I add the first piece of the code that loads the button into
the toolbar, the addin no longer works. Anything related to the
ActiveExplorer will kill the addin silently upon loading. No amount
of try/catching or if/elsing will prevent the failure, and print
statements no longer get executed no matter where they're placed in
OnStartupComplete.

Now, if I forget about trying to load the button into the toolbar and
instead call my code directly from the OnConnection event, the program
itself comes up and works flawlessly as soon as the user opens
Outlook. Of course, once they close the program, it becomes
unavailable until they restart Outlook because there's no button,
which is less than ideal. But it at least shows that the code itself
isn't the problem. There seems to be some kind of malfunction when
trying to access the ActiveExplorer, almost as if it doesn't exist at
the point which it's being called.

Outlook itself is not being opened via ActiveSync or any other method
that I'm aware of -- I'm just opening it like any of us would, so
there should be a UI available, yes? I've pin-pointed exactly where
the addin fails to load, but I'm completely lost as to how I should
get around this. Below is my code from the Connect class -- there's
nothing terribly different about it from what I've seen in other
posts, but maybe there's something blaring there that I'm missing.
Any assistance that ANYONE can give at this point would be most
appreciated.

Thank you all in advance,
Steve

=========================================

Imports Microsoft.Office.Core
Imports Extensibility
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Core.MsoControlType
Imports Microsoft.Office.Core.MsoBarPosition
Imports Microsoft.Office.Core.MsoButtonStyle
Imports Microsoft.Office.Core.MsoFileDialogType
Imports Microsoft.Office.Core.MsoFileDialogView
Imports Microsoft.Office.Core.MsoTriState

<GuidAttribute("5ccfec6f-5ea3-48d6-92ac-192842cf866b"),
ProgIdAttribute("MyWizard.Connect")> _
Public Class Connect
Implements Extensibility.IDTExtensibility2

Dim applicationObject As Object
Dim addInInstance As Object
Dim WithEvents objCommandBarButton As CommandBarButton

Public Sub OnBeginShutdown(ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnBeginShutdown
'On Error Resume Next
objCommandBarButton.Delete()
objCommandBarButton = Nothing
End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnStartupComplete
DisplayMsgBox("on startup complete running")
'DisplayMsgBox("on startup complete running")
Dim objCommandBars As CommandBars
Dim objCommandBar As CommandBar
Dim objCommandBarControl As CommandBarControl

' Create a menu command on the "Tools" menu.
Try
objCommandBars =
applicationObject.ActiveExplorer.CommandBars
'DisplayMsgBox("menu command works")
Catch ex As Exception
'DisplayMsgBox(ex.Message)
objCommandBars = applicationObject.CommandBars
End Try

Try
objCommandBar = objCommandBars.Item("Standard")
'DisplayMsgBox("standard menu works")
Catch ex As Exception
objCommandBar = objCommandBars.Add("MyWizard",
msoBarFloating, , True)
'DisplayMsgBox("floating menu should be created")
End Try
objCommandBar.Visible = True

'Make sure menu command doesn't already exist.
For Each objCommandBarControl In objCommandBar.Controls
If objCommandBarControl.Caption = "MyWizard" Then
objCommandBar.Controls.Item("MyWizard").Delete()
End If
Next objCommandBarControl

objCommandBarButton =
objCommandBar.Controls.Add(msoControlButton, , , , True)
With objCommandBarButton
.Caption = "MyWizard"
.Style = msoButtonCaption
.Tag = "MyWizard"
.OnAction = "<!MyWizard.Connect>"
.Visible = True
End With
End Sub

Public Sub OnDisconnection(ByVal RemoveMode As
Extensibility.ext_DisconnectMode, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnDisconnection
On Error Resume Next

' Disconnect the shared add-in, no matter how the host
application
' was shut down.
If RemoveMode <>
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown Then _
Call OnBeginShutdown(custom)

applicationObject = Nothing
End Sub

Public Sub OnConnection(ByVal application As Object, ByVal
connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As
Object, ByRef custom As System.Array) Implements
Extensibility.IDTExtensibility2.OnConnection
applicationObject = application
addInInstance = addInInst

' No matter how the host application started, connect the
shared add-in.
If (connectMode <>
Extensibility.ext_ConnectMode.ext_cm_Startup) Then _
Call OnStartupComplete(custom)
End Sub

Private Sub objCommandBarButton_Click(ByVal Ctrl As
CommandBarButton, _
ByRef CancelDefault As Boolean) Handles
objCommandBarButton.Click

' These are the two functions that initialize the wizard.
' The program works fine when these are called from
OnConnection.
ConfigureEnv()
RunPasswordForm()

End Sub

End Class


=========================================
 
Normally, in .NET or in unmanaged code you want to add your UI for Explorers
in the first Activate event of the Explorer. To test for a UI you may want
to not check for ActiveExplorer and instead test for
applicationObject.Explorers.Count.

You also can't always expect Outlook not to be started from automation code
with no UI, so you have to account for that.

Some versions of Outlook won't fire an Explorer.Activate event on that first
startup Explorer, so also include in your code event handlers for
SelectionChange and BeforeFolderSwitch. That way you will always get your UI
created.

When and where Explorers and Inspectors are instantiated can vary from
version to version, so use Activate events rather than NewExplorer or
NewInspector.
 
Thanks Ken,

I'm trying the following now, but on my dev machine it's failling on
[objCommandBar = objCommandBars.Item("Standard")] saying that "Object
reference is not set to an instance of an object." I can only get it
to run successfully if I set [objCommandBars =
applicationObject.ActiveExplorer.CommandBars]:

==============================

Dim expl As Outlook.Explorer
Dim objCommandBars As CommandBars
Dim objCommandBar As CommandBar
Dim objCommandBarControl As CommandBarControl

Try
If applicationObject.Explorers.Count >= 1 Then
expl = applicationObject.Explorers.Item(1)
objCommandBars = expl.CommandBars
End If
Catch ex As Exception
DisplayMsgBox(ex.Message)
End Try

'Load the button into the standard Outlook toolbar
Try
objCommandBar = objCommandBars.Item("Standard")
Catch ex As Exception
objCommandBar = objCommandBars.Add("WebAideSynch",
msoBarFloating, , True)
End Try
objCommandBar.Visible = True

==============================

The objCommandBars isn't Nothing at the point where it's failing.
Thoughts?

Thanks,
Steve
 
I'm also unable to get the objCommandBars.Count or see what's actually
inside it. It only seems to like when I feed it the ActiveExplorer.

-Steve
 
I usually just check for applicationObject.Explorers.Count in OnConnection
and if > 0 I add the Explorer to a sorted List collection. I do that with an
Explorer wrapper class that is the event handler for events fired in that
Explorer (Activate, Close, SelectionChange, maybe some item events). I add
the Explorer instance there also and create my UI in the class when either
Activate, SelectionChange or BeforeFolderSwitch fires).

Something like this:

Public Sub OnConnection(ByVal application As Object, ByVal connectMode
As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As
System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

m_blnTeardown = False

addInInstance = Nothing
addInInstance = TryCast(addInInst, Office.COMAddIn)

If (addInInstance IsNot Nothing) Then
'set module level reference to COMAddIn object
Try
m_objOutlook = CType(application, Outlook.Application)

'event-aware reference to Explorers collection
'use NewExplorer event to watch for UI creation
m_colExplorers = m_objOutlook.Explorers

Try
'put ProgID in a global variable
g_strProgID = addInInstance.ProgId

addInInstance.Object = Me

'Are we starting with UI?
If m_colExplorers.Count > 0 Then
'we have UI
InitHandler()

If m_blnInit = True Then
AddExpl(m_olExplorer) 'add to wrapper class
collection
End If

m_objNS = m_objOutlook.GetNamespace("MAPI")

m_colInspectors = m_objOutlook.Inspectors
Else
'do nothing
'monitor Explorers collection (in this module)
'if NewExplorer event is raised then we have UI
End If
Catch ex As Exception
TearDown()
End Try

Catch ex As Exception
TearDown()
End Try
Else
TearDown()
End If
End Sub
 
Back
Top