anything wrong with this code?

S

song

Here is the piece of code in a COM add in for Outlook. It prompts a msgbox
asking if
the user wants to exit an inspector or not. If not, cancel the operation.

Everything goes well, however outlook stays in memory every time after I
cancel the exit.
It seems there is something I did not release. But if I did not cancel the
exit operation, it is ok.

What is wrong with this? Thank you.


Private Sub objApptItem_Close(Cancel As Boolean)
On Error Resume Next
If objApptItem.Links.Count = 0 Then
If MsgBox("This appointment has no contact related, do you want to
exit?",_
vbOKCancel, _
"Appointment Exit") = vbCancel Then
Cancel = True
End If
End If
End Sub
 
K

Ken Slovak - [MVP - Outlook]

See the ItemsCB COM addin sample in VB 6 to see how to properly release all
your Outlook objects so Outlook can close. If an Inspector object or Item
object is still unreleased when Outlook tries to close the On_Disconnection
event for the addin won't fire and Outlook won't close. ItemsCB can be
downloaded from the Resources page at www.microeye.com
 
S

song

Thank you very much. I downloaded ItemsCB and did what it indicated.
The problem is still there.
Outlook will quit well when I don't cancel the close action of appointment.
But it will stay in memory if I cancel.

What problem might be?
 
K

Ken Slovak - [MVP - Outlook]

My guess is that you aren't releasing all your Outlook objects. That's
almost always the cause of Outlook not shutting down. If the appointment
Close event is canceled how does the appointment ever get closed? If and
when it is closed are you releasing its object variable and any related
sub-objects or the related Inspector?
 
S

song

Two steps: 1. I cancel the close event of appointment
2. Then close it later on. So that outlook can be closed.
Outlook remains in memory after the two steps.

But if I close it directly(no cancel action), it can be cleared.

I checked, all the objects are set to nothing in UnInitHandler.

What possible reason for this?

Thank you
 
K

Ken Slovak - [MVP - Outlook]

Again, I'm not sure. Are you handling any possible errors in your code?
Unhandled errors also cause Outlook to remain in memory. Are you sure you
are releasing all your Inspector objects? Can you step the code and check
the Inspectors and Explorers collections in your UnInitHandler?
 
S

song

Thank you for the patient analysis.

I don't have much error handling code.
My code mostly starts with " On Error Resume Next"

I stepped the code, found the explorers have 1 item left, inspectors are
empty.

Ken Slovak - said:
Again, I'm not sure. Are you handling any possible errors in your code?
~~~~~~~~ This might be the
reason.
 
K

Ken Slovak - [MVP - Outlook]

On Error Resume Next is bare-bones but sufficient as a minimal handler of
errors.

When you are running your shut-down code there should be no Explorers left.
In the ExplorerClose event you should be testing something like this:

Private Sub objExpl_Close()
On Error Resume Next

'Current Explorer is closing--update identity to ActiveExplorer
Set objExpl = g_objOL.ActiveExplorer
'if this is the last Explorer, then objExpl = Nothing ->close down
If (objExpl Is Nothing) And (g_objOL.Inspectors.Count = 0) Then
UnInitHandler
End If
End Sub
 
S

song

Thanks

I had similar code there, and it shows ActiveExplorer Is Nothing and then
calls uninitHandler.
In that procedure, I found one item still left in Explorere Collection.

Is it strange?
 
K

Ken Slovak - [MVP - Outlook]

That sounds strange to me.

When my typical COM addin code runs first an Explorer Close event fires in
my Explorer wrapper class. At that point Explorers.Count = 0. I then call
UnInitHandler from there.

After that the main Explorer Close event fires but since the UnInitHandler
has run already once all the objects have been released and I don't need to
run UnInitHandler again. I use the second Close event in the main class
module just in case for some reason the Explorer wrapper one hadn't fired.
I'm over conservative in my Outlook coding, belts and suspenders everywhere
since I've been bitten by unexpected oddities many times in Outlook code.

Are you using an Explorer wrapper collection and are you sure that you have
added every Explorer to it if you are?
 
S

song

Thank you so much, appreciate that.
Now, you can reproduce the oddity easily as following:

Use the template at following link to start a program in VB6.
http://www.microeye.com/resources/Outlook COM Addin.zip
I programmed a minimum sample to show that the template has some problem
Do three things:

1. uncomment " Set objApptItem = objItem" in "colInsp_NewInspector"
2. Add code "On Error Resume Next
If objApptItem.Links.Count = 0 Then
If MsgBox("This appointment has no contact related, do you want to
exit?", _
vbOKCancel, "Appointment Exit")
= vbCancel Then
Cancel = True
End If
End If"
to objApptItem_Close event
3. "Set objApptItem = Nothing" in UnInitHandler

Start Outlook 2000.

Then you can find the strange thing I said.
I can not figure out what is wrong for three days :((
 
K

Ken Slovak - [MVP - Outlook]

In the UnInitHandler code the first time it runs (it ends up running twice,
although that doesn't cause a problem) the count of Explorers is 1 even
though there is no ActiveExplorer. However, I still don't see Outlook
remaining in memory. I tested on Outlook 2000 SP3 running on Windows 2000
Professional SP4 with an Exchange profile.
 
S

song

My platform is Outlook 2000 SP3 running on Windows 2000 Server. No exchange.

I run it this way:
Open an appointment with contact,
try to close it, then it prompts question.
Cancel it.
Try to close it again, click ok to close it.

Outlook will remain, everytime in my computer.
If click ok in the first place, it quits well.

Traced into it, after click cancel, then click close,
in this scenario, UnInitHandler only called
once, because On_disconnection was not fired.
 
K

Ken Slovak - [MVP - Outlook]

I tried canceling the closing, then closing it. I also tried just closing it
without canceling. In neither case did Outlook remain in memory. I didn't
even use my own templates for COM addins, I downloaded the one from MicroEye
and used that with the changes you suggested. In no case did Outlook remain
in memory when I closed it down.

Just out of curiosity are you using Outlook 2000 in Internet only mode
(check in Help, About)? I use corporate/workgroup mode. Perhaps that's the
difference. Internet only mode has some severe limitations and has a
crippled MAPI library.
 
S

song

Yes, you are right, it is Internet mail only mode.
Maybe that is the problem. But most users are using this type
of mode,right?
How to change it? To some "strong" type?

Thanks
 
K

Ken Slovak - [MVP - Outlook]

I don't know about "most" users. I would think that it is actually a
minority of users of Outlook 2000 since most are on corporate systems where
Exchange is used. And that mode doesn't exist at all in Outlook 2002 or
2003.

You can convert modes from Internet only to corporate/workgroup mode. For
information about that and what features you gain and what you lose, see
http://www.slipstick.com/outlook/choosingmode.htm
 
S

song

Yes, I changed to corporate/workgroup mode.

Tested again, no luck.
Normally, Outlook shows a window says outlook is exiting, please wait.
But if the add-in was activated, cancel and close, then even that
window won't appear.

The only difference between your configuration and mine is OS now,
maybe that is the problem? I will test it in my home computer,
which has Windows 2000 professional, this evening.

Did anyone else have such problem before?

Thank you very much. I have a great impression on MVP now.
 

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