A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Deleting Mail Items - Code



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old November 25th 08, 02:26 PM posted to microsoft.public.outlook.program_vba
Nigel RS
external usenet poster
 
Posts: 16
Default Deleting Mail Items - Code

The following snippet of VBA code (Outlook 2003) is used to selectively
delete mail items from a selected mail folder. where the mail item subject
matches a predefined string (MailSubject). This code works great except it
fails to delete the last item. If there are 10 items matching it will delete
9 but leave one behind. If there is only 1 it fails to delete it.

If I step the code in the debugger it works! What could be causing this?

For Each objItem In objFolder.Items
' delete the item from the folder
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If
Next

Ads
  #2  
Old November 25th 08, 02:49 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Deleting Mail Items - Code

Don't use For Each loops or up counting For loops for deleting items from a
collection. Use For loops that count down (Step -1) or use Do loops that
test for no items left.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
The following snippet of VBA code (Outlook 2003) is used to selectively
delete mail items from a selected mail folder. where the mail item
subject
matches a predefined string (MailSubject). This code works great except
it
fails to delete the last item. If there are 10 items matching it will
delete
9 but leave one behind. If there is only 1 it fails to delete it.

If I step the code in the debugger it works! What could be causing this?

For Each objItem In objFolder.Items
' delete the item from the folder
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If
Next


  #3  
Old November 25th 08, 04:30 PM posted to microsoft.public.outlook.program_vba
Nigel RS
external usenet poster
 
Posts: 16
Default Deleting Mail Items - Code

Thanks Ken, I guess it is similar to deleting rows in Excel. I will change
it to a down counter.

Cheers

"Ken Slovak - [MVP - Outlook]" wrote:

Don't use For Each loops or up counting For loops for deleting items from a
collection. Use For loops that count down (Step -1) or use Do loops that
test for no items left.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
The following snippet of VBA code (Outlook 2003) is used to selectively
delete mail items from a selected mail folder. where the mail item
subject
matches a predefined string (MailSubject). This code works great except
it
fails to delete the last item. If there are 10 items matching it will
delete
9 but leave one behind. If there is only 1 it fails to delete it.

If I step the code in the debugger it works! What could be causing this?

For Each objItem In objFolder.Items
' delete the item from the folder
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If
Next



  #4  
Old November 25th 08, 04:58 PM posted to microsoft.public.outlook.program_vba
Nigel RS
external usenet poster
 
Posts: 16
Default Deleting Mail Items - Code

Hi Ken
I modified my code as suggested by using a count down structure, however it
still does not delete the last item that meets the criteria !! Stepping
through the code the test is applied and the delete step is run, but the last
mailitem remains in the folder! Any ideas?

For iItem = objFolder.Items.Count To 1 Step -1

Set objItem = objFolder.Items(iItem)

' delete the item from the inbox
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If

Next iItem

"Ken Slovak - [MVP - Outlook]" wrote:

Don't use For Each loops or up counting For loops for deleting items from a
collection. Use For loops that count down (Step -1) or use Do loops that
test for no items left.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
The following snippet of VBA code (Outlook 2003) is used to selectively
delete mail items from a selected mail folder. where the mail item
subject
matches a predefined string (MailSubject). This code works great except
it
fails to delete the last item. If there are 10 items matching it will
delete
9 but leave one behind. If there is only 1 it fails to delete it.

If I step the code in the debugger it works! What could be causing this?

For Each objItem In objFolder.Items
' delete the item from the folder
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If
Next



  #5  
Old November 25th 08, 08:16 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Deleting Mail Items - Code

See if adding a DoEvents after you delete the item in the loop helps with
that.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
Hi Ken
I modified my code as suggested by using a count down structure, however
it
still does not delete the last item that meets the criteria !! Stepping
through the code the test is applied and the delete step is run, but the
last
mailitem remains in the folder! Any ideas?

For iItem = objFolder.Items.Count To 1 Step -1

Set objItem = objFolder.Items(iItem)

' delete the item from the inbox
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If

Next iItem


  #6  
Old November 28th 08, 11:58 PM posted to microsoft.public.outlook.program_vba
Penny
external usenet poster
 
Posts: 3
Default Deleting Mail Items - Code

Nigel RS wrote:
For iItem = objFolder.Items.Count To 1 Step -1


try .. For iItem = objFolder.Items.Count To 0 Step -1

  #7  
Old December 1st 08, 01:54 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Deleting Mail Items - Code

Outlook collections always run from 1 to number, not from 0.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Penny" wrote in message
m...
Nigel RS wrote:
For iItem = objFolder.Items.Count To 1 Step -1


try .. For iItem = objFolder.Items.Count To 0 Step -1


  #8  
Old December 4th 08, 07:35 AM posted to microsoft.public.outlook.program_vba
Nigel[_2_]
external usenet poster
 
Posts: 4
Default Deleting Mail Items - Code

Hi Ken
Sorry for delay, I tried adding DoEvents without success.

--

Regards,
Nigel




"Ken Slovak - [MVP - Outlook]" wrote in message
...
See if adding a DoEvents after you delete the item in the loop helps with
that.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel RS" wrote in message
...
Hi Ken
I modified my code as suggested by using a count down structure, however
it
still does not delete the last item that meets the criteria !! Stepping
through the code the test is applied and the delete step is run, but the
last
mailitem remains in the folder! Any ideas?

For iItem = objFolder.Items.Count To 1 Step -1

Set objItem = objFolder.Items(iItem)

' delete the item from the inbox
If Left(objItem.Subject, mSubLen) = MailSubject Then
objItem.Delete
End If

Next iItem



  #9  
Old December 4th 08, 02:43 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Deleting Mail Items - Code

OK, then you might have to add a test after the loop to see if there's still
any items left in that collection and if so try either getting that last
item as an object item and calling its Delete() method again or using
Items.Remove(1) and see if either of those works.

In some versions of Outlook removing the last item in a collection using
Delete() doesn't work. That might be what you're running into here.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Nigel" wrote in message
...
Hi Ken
Sorry for delay, I tried adding DoEvents without success.

--

Regards,
Nigel


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting items Dan D. Outlook - Calandaring 2 September 29th 08 04:58 PM
Deleting individual mail items from Outlook 2007 archive beemer Outlook - General Queries 6 May 7th 08 12:40 PM
Can I prevent others from deleting calendar items? dlongley Outlook - Calandaring 4 March 27th 08 03:44 PM
Deleting sent items from outlook 2003 curtis888 Outlook and VBA 2 May 4th 07 04:49 PM
Deleting calendar items should ask "all past and future items" katie Outlook - Calandaring 0 April 4th 06 03:52 AM


All times are GMT +1. The time now is 11:37 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.