Probem with Com add-in

B

Bjørn Eliasen

Hi.

I have been working with this problem intensively. Below please find some
sample code which has the unwanted behavior. Compile and install the code
(the problem does not show when debugging), open a mail item, and then close
Outlook without closing the mail item. Outlook will now hang. Any
suggestions will be highly appriciated.

I use vb 6.0 and Outlook 2003 but the problem also shows in Outlook XP.

The code below is based on the Outlook Comm add-in template from Micro Eye
Inc. Thanks to Micro Eye Inc.

Best regards, Bjørn

Code:

In Connect.dsr (From MicroEye COM Add-in template, Unchanged):
Option Explicit
Private gBaseClass As New OutAddIn
Private Sub AddinInstance_OnAddInsUpdate(custom() As Variant)
'DebugWrite "AddinInstance_OnAddInsUpdate"
End Sub
Private Sub AddinInstance_OnBeginShutdown(custom() As Variant)
'
'DebugWrite "AddinInstance_OnBeginShutdown"
End Sub
Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
On Error Resume Next
'Evaluate ConnectMode
Select Case ConnectMode
Case ext_cm_Startup
Case ext_cm_AfterStartup
Case ext_cm_CommandLine
Case ext_cm_Startup
End Select
If Application.Explorers.Count = 0 And Application.Inspectors.Count = 0
Then
Exit Sub
End If
gBaseClass.InitHandler Application, AddInInst.ProgId
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
gBaseClass.UnInitHandler
If RemoveMode = ext_dm_UserClosed Then
Else
End If
Set gBaseClass = Nothing
End Sub
Private Sub AddinInstance_OnStartupComplete(custom() As Variant)
'DebugWrite "AddinInstance OnStartupComplete"
End Sub

In OutAddIn.cls

Option Explicit
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
Private WithEvents objMailItem As Outlook.MailItem

Friend Sub InitHandler(olApp As Outlook.Application, strProgID As String)
Set objOutlook = olApp 'Application Object
Set m_olApp = olApp
Set objNS = objOutlook.GetNamespace("MAPI") 'NameSpace Object
Set colExpl = objOutlook.Explorers 'Explorers Object
Set colInsp = objOutlook.Inspectors 'Inspectors Object
Set objExpl = objOutlook.ActiveExplorer 'Explorer Object
End Sub

Friend Sub UnInitHandler()
Set objInsp = Nothing
Set objExpl = Nothing
Set objMailItem = Nothing
Set colInsp = Nothing
Set colExpl = Nothing
Set objNS = Nothing
Set m_olApp = Nothing
Set objOutlook = Nothing
End Sub

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

Private Sub colInsp_NewInspector(ByVal inspector As inspector)
Dim objItem As Object
On Error GoTo 0

Set objInsp = inspector
Set objItem = objInsp.CurrentItem
Select Case objItem.Class
Case olMail
Set objMailItem = objItem
Case Else
End Select
End Sub

Private Sub objExpl_BeforeFolderSwitch(ByVal NewFolder As Object, Cancel As
Boolean)
On Error Resume Next
End Sub

Private Sub objExpl_Close()
Set objExpl = m_olApp.ActiveExplorer
If (objExpl Is Nothing) And _
(m_olApp.Inspectors.Count = 0) Then
UnInitHandler
End If
End Sub
Private Sub objInsp_Close()
If m_olApp.ActiveExplorer Is Nothing And _
m_olApp.Inspectors.Count <= 1 Then
UnInitHandler
End If
Set objInsp = Nothing
End Sub

Private Sub objMailItem_Close(Cancel As Boolean)
frmTest.Show 1 ' This is where Outlook hangs
End Sub

In Module modOutlook.bas
Option Explicit
Public m_olApp As Outlook.Application

In frmTest.frm with one button named brOK:

Private Sub btOk_Click()
Unload Me
End Sub
 
K

Ken Slovak - [MVP - Outlook]

Other than not opening that form modally the only thing I can think of would
be complicated.

It would involve setting up a Windows message hook handler and callbacks for
it and intercepting calls to shut Outlook down and then releasing the modal
form if you got such a call. You'd have to trace the open windows and
procedures in Outlook to get a handle to the Outlook process and look for
the shutdown messages directed to that process.
 
B

Bjørn Eliasen

Thanks.

I am afraid you're rigth.

Do you know of any links to somewhere, where Windows message hook's are
explained in some details - preferably with some sample code? The error
below occurs in a central add-in which is part of a larger project being
implemented for more that 700 people which implies that I could spend some
time solving this.

Best regards,

Bjørn
 
K

Ken Slovak - [MVP - Outlook]

There are a number of different implementations for Windows message hooks,
some use a form and some use a class module to handle up the callback. There
are also 3rd party hooks that are available already compiled into OCX's,
some are free and some cost some money.

One that I've used a number of times with some modifications is at
http://vbnet.mvps.org/. Click the show all code link and then click in the
"hooks" link at the top of the page to get to teh Windows Hooks Routines
page.

When Outlook is open with a UI the main window is named "rctrl_renwnd32".
The main Outlook process is can be gotten by using Win32 API calls to
GetCurrentProcessId and GetCurrentThreadId. You can use that in the
On_Connection event handler as follows to get the Outlook process id and
thread id:

Private Declare Function GetCurrentProcessId Lib "Kernel32.dll" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

'get Outlook ProcessID (same as current process ID since Add-In runs
in-process
lngPID = GetCurrentProcessId
lngThreadID = GetCurrentThreadId
 
B

Bjørn Eliasen

Thanks. I'll give it a try.

Bjørn

Ken Slovak - said:
There are a number of different implementations for Windows message hooks,
some use a form and some use a class module to handle up the callback. There
are also 3rd party hooks that are available already compiled into OCX's,
some are free and some cost some money.

One that I've used a number of times with some modifications is at
http://vbnet.mvps.org/. Click the show all code link and then click in the
"hooks" link at the top of the page to get to teh Windows Hooks Routines
page.

When Outlook is open with a UI the main window is named "rctrl_renwnd32".
The main Outlook process is can be gotten by using Win32 API calls to
GetCurrentProcessId and GetCurrentThreadId. You can use that in the
On_Connection event handler as follows to get the Outlook process id and
thread id:

Private Declare Function GetCurrentProcessId Lib "Kernel32.dll" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

'get Outlook ProcessID (same as current process ID since Add-In runs
in-process
lngPID = GetCurrentProcessId
lngThreadID = GetCurrentThreadId
 

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