Update KB924085 causes MailItem.delete to fail

J

j.dowling

Hi there,

I've written some custom Outlook VBA code which is executed when a
mailitem is closed. The logic when a user closes a mailitem is:
1. The user is prompted for a server file location to save the email,
2. The email is saved to that location,
3. And the email is deleted from the inbox.

The idea is to force staff at this law company to store emails
centrally and automatically clean up their Inbox folder.

This has worked nicely for the past few years with Outlook 2000 and
2003. However since the outlook security update KB924085 the email
cannot be deleted. The mailitem Close and Move events also fail. The
error is "Run time error '-417070840 (e7240108)': Unable to complete
the command.".

If I uninstall the update everything is fine.

Has anyone else had this problem and worked around it? At the moment I
can only save the email after updating the flagicon and updating the
subject line to "***please delete me***".

Thanks,
Jason
 
J

j.dowling

Thanks - I'll download a copy. I'm suprised that a 3rd party library
is required to achieve something as basic as deleting an email through
VBA code after one security update? The strange thing also is that
Outlook allows me to delete attachments from emails using VBA, but not
delete the email itself!

Cheers,
Jason
 
K

Ken Slovak - [MVP - Outlook]

Does this code use item.Delete in either the item.Close or Inspector.Close
event?

The word is that in those events using .Close or .Delete or .Move caused
crashes on some occasions so MS disabled those commands from working in
those event handlers. That's specific for Outlook 2007. There has been no
word about the same thing happening with the hot fix, but it looks like they
did the change there too. If that's the case then you have to change your
code or uninstall that hot fix.

I'm attempting to find out about this.
 
J

j.dowling

Thanks for your help. Yes, the code is in the mailitem.close event.
So I guess I need to try and work around this. Uninstalling the update
works in the short-term but it just gets re-installed unless I disable
automatic updates (which isn't really an option). There is also a
thread (and source code showing the problem) on Sue Mosher's site about
this problem at
http://www.outlookcode.com/threads.aspx?forumid=2&messageid=21363 .

Cheers,
Jason
 
K

Ken Slovak - [MVP - Outlook]

Yes, Sue and I have been working on this.

You really can't expect that the update won't be installed or that the
changes will be rolled back, so you have to adapt your code.

What I set up as a workaround is to set a global flag and I add the EntryID
of the item I want deleted into a comma separated string variable that has
the list of items to be deleted (in case more than 1 item was opened). A
timer fires every 30 seconds and checks the flag and the list to see if
anything needs to be deleted. If so it gets deleted then and the EntryID
removed from the list if there are no errors in the deletion.

That seems to work with or without the update installed.
 
J

j.dowling

Thanks again for your help. I was thinking that the following might
also do the trick: 1. After each email is saved to an .msg file I set a
flag and change the subject line to "**Scheduled for deletion***". 2.
When Outlook is closing, I just loop through and delete any email with
those properties? I haven't had a chance to test this out yet...

Thanks,
Jason
 
K

Ken Slovak - [MVP - Outlook]

I'd go with the timer myself.

If you wait for Explorer.Close on the final Explorer I wouldn't bet that
everything I needed to get to all the items that needed deletion would be
available. If you wait for OnDisconnection in an addin or BeginShutdown or
for Application.Quit in VBA code everything Outlook is already released and
out of scope.

Also, I'm not following something here. Why would you want to save
everything as a MSG file in the file system?
 
J

j.dowling

Thanks for your comments - I'll try out the Timer and let you know how
it goes.

Why save emails as msg files? So they can clean up their Inboxes in
Outlook, share emails between staff, and have messages located on
their server in directories which relate to their paper files. They
also stored scanned correspondence and generated legal document in
these directories as well. If the email has attachments: the user
gets asked which file they relate to; the attachment is removed, and a
string indicating the save path is inserted at the end of the email
body. So it does organise files for them and it is cheap.

Disclaimer: This is not my design (I just do some part-time VB/VBA
contracting there) and I wouldn't recommend it (eg. files get updated
or deleted, there is no change control/audit history, searching is
difficult, etc ). If they wanted to spend the money I could do
something much more robust and usable in Exchange, Notes, or an
RDBMS.

Thanks,
Jason.
 
J

j.dowling

I have this working now. To get around the security update I've used
the following code to clean up emails which have been scheduled for
deletion (the subject and flag can still be updated in the
mailitem.close event:

1. In ThisOutlookSession.Application_Quit add the following line:
Call globals.removeSavedEmails

2. In globals add the following function:

Public Function removeSavedEmails()

On Error GoTo errTrap
Dim olns As Outlook.NameSpace
Dim ol As Outlook.Application
Dim olInbox As Outlook.MAPIFolder
Dim userItems As Outlook.Items
Dim useritem As Outlook.MailItem

Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set olInbox = olns.GetDefaultFolder(olFolderInbox)

On Error Resume Next 'needed because useritem count decreased for
each deleted email!

For Each useritem In olInbox.Items
If useritem.FlagIcon = olPurpleFlagIcon Then
If useritem.Subject = "****DELETED****" Then
useritem.Delete
End If
End If
Debug.Print useritem.Subject
Next

On Error GoTo errTrap

Exit Function
errTrap:
MsgBox "Error in Globals.removeSavedEmails(). Error number: " &
Err.Number & ", " & Err.Description & " has occured."
End Function
 
K

Ken Slovak - [MVP - Outlook]

If you are deleting items (or moving them) from a collection do not use a
For Each or count up For loop. As each item is deleted the collection
contents are updated and often you end up deleting only 1/2 of the items in
the collection. Always use a count down For loop for that sort of thing:
For i = col.Count To 1 Step -1
 

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