COM-Addin created menu doesn't show

P

Peter Karlström

Hi

I'm developing a COM Addin which creates a menu in Outlooks Tools menu.

All works fine, but when I start my PDA the Microsoft ActiveSync triggers a
new Outlook session (which is a hidden one).
Then when I start Outlook the ordinary way, the COM Addin has not created the
menu, because the Addin has been disabled.

I have looked around the object model for a way to find out in which state
Outlook is running, i.e. hidden or shown. I thought that creating the menu
when Outlook is
not visible causes the error, and that I could skip the code if that was the
case.

But maybe I'm on the wrong track?

Have anybody seen this before, and can give me a solution?

Regards
 
P

Peter Karlström

Hi again

A small correction of the behaviour:

The menu is NOT created then the first instance of Outlook is created from
ActiveSync, i.e. in hidden mode.

How can I catch the event of a new instance of Outlook in the AddIn?

Thanks in advance
 
K

Ken Slovak - [MVP - Outlook]

It's not really a new instance of Outlook, only 1 can be running at a time.
It's that the Outlook instance would now have at least one member of the
Explorers collection.

The old AddInMon type workaround we used for years no longer works correctly
with some of the newer synch software, so the best way seems to be to handle
things inline in the addin code. Here's an outline of what we've come up
with (David Kane of MicroEye and myself):

In the Designer class or whatever class is handling IDTExtensibility2:

Private gBaseClass As New clsAddin 'the general class that handles init and
shutdown

'reference to trusted Application object
Private mobjOutlook As Outlook.Application

Private mblnTeardown As Boolean

'Event-aware references to Explorers collection & ActiveExplorer
Private WithEvents mcolExplorers As Outlook.Explorers
Private WithEvents molExplorer As Outlook.Explorer

Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
'
On Error Resume Next
End Sub

Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
'
On Error Resume Next

TearDown
End Sub

Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)

Dim strPath As String

On Error Resume Next

strPath = App.Path

'set module level reference to COMAddIn object
Set mobjOutlook = Application

'event-aware reference to Explorers collection
'use NewExplorer event to watch for UI creation
Set mcolExplorers = mobjOutlook.Explorers

'put ProgID in a global variable
gstrProgID = AddInInst.ProgId

AddInInst.Object = Me

mblnTeardown = False

'Are we starting with UI? - key section here
If mcolExplorers.Count > 0 Then
Set molExplorer = mobjOutlook.Explorers.Item(1)
'we have UI - initialize base class
gBaseClass.InitHandler mobjOutlook, AddInInst.ProgId
Else
'do nothing
'monitor Explorers collection (in this module)
'if NewExplorer event is raised then we have UI
End If
End Sub

Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
'Tear down the class
'IMPORTANT: This event will not fire when
'RemoveMode = ext_dm_HostShutdown
'It will fire when RemoveMode = ext_dm_UserClosed

On Error Resume Next

TearDown
End Sub

Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
'
On Error Resume Next
End Sub

' Release object references and remove message hook
Private Sub TearDown()
On Error Resume Next

mblnTeardown = True

'Tear down the base class
If gBaseClass.Init = True Then
gBaseClass.UnInitHandler 'release all Outlook objects here
DoEvents
End If
Set gBaseClass = Nothing

'release reference to Outlook objects
Set molExplorer = Nothing
Set mcolExplorers = Nothing
Set mobjOutlook = Nothing
End Sub

' NewExplorer event will be raised if there is UI
Private Sub mcolExplorers_NewExplorer(ByVal Explorer As Outlook.Explorer)
On Error Resume Next

'assign ActiveExplorer
Set molExplorer = Explorer

If gBaseClass.Init = False Then
'we didn't have UI before - initialize add-in objects
gBaseClass.InitHandler mobjOutlook, gstrProgID 'instantiate all objects
End If
End Sub

' Monitor Explorer_Close to see when UI "disappears"
Private Sub molExplorer_Close()
On Error Resume Next

'release current reference
Set molExplorer = Nothing

Set molExplorer = mobjOutlook.ActiveExplorer
If (molExplorer Is Nothing) And (mobjOutlook.Inspectors.Count = 0) Then
'release add-in objects
gBaseClass.UnInitHandler
End If
End Sub

Then in the InitHandler class module (clsAddin):

'Object variables for Event procedures
Private WithEvents objOutlook As Outlook.Application
Private WithEvents objNS As Outlook.NameSpace
Private WithEvents objExpl As Outlook.Explorer
Private WithEvents colExpl As Outlook.Explorers
Private WithEvents objInsp As Outlook.Inspector
Private WithEvents colInsp As Outlook.Inspectors

'Initialization flag
Private mblnInit As Boolean

Private mblnFailed As Boolean

Private mblnRemindersSetUp As Boolean

'Expose Init property to Connect class
Friend Property Get Init() As Boolean
Init = mblnInit
End Property

' Init here
Friend Sub InitHandler(OLApp As Outlook.Application, strProgID As String)
'whatever
End Sub

Friend Sub UnInitHandler()
'destroy everything here

'at end
mblnInit = False
End Sub

' Make sure to call UninitHandler when count of Explorers goes to <= 1 in
Explorer.Close event. Also check for Inspectors.Count = 0 there and repeat
tests in Inspector.Close.
 
P

Peter Karlström

Thanks a lot Ken

I will try this out.

Best Regards
--
Peter Karlström
Midrange AB
Sweden
 

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