More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

Login

login
password
Remember me

You will need to register and log in if you want to download the source code for the Microsoft Outlook Programming book. The forums and code sharing areas are open to both registered and non-registered visitors.

forum 09-Sep-2010 06:22

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

 

    Page [ 1 ]  
 Basic Outlook Printer Friendly Version
Working with Outlook items, folders, recipients; dealing with security; writing event handlers
Topic
Strange behaviour with Restrict on Shared Folder
Hi All,
I hope that someone hasn't posted about this before but I am using Outlook 2003 VBA to find all appointments after a certain date and delete them. When I run this code on my own appointments it finds all the appointments fine and deletes them. However, when I run it on a shared folder - it doesn't find all of the appointments (identical to those in my folder) on the first run. It seems to miss the last 2 appointments. If I run the function again, it finds one of the 2 extra ones and then if I run it again, it finds the last one.
Have I missed something?
Heres my code:
         strRestriction = "[End] >='" & Format$(datStart, "dd/mmm/yyyy ") & " 00:00'"
         
         Dim oResItems
         
         Set oResItems = oItems.Restrict(strRestriction)

         oResItems.Sort "[Start]"
         Dim oAppt As AppointmentItem
         For Each oAppt In oResItems
             If InStr(1, oAppt.Categories, G_Flight_Category) > 0 Then
                 'Delete all appointments with category Flights
                 oAppt.Delete
                 intCount = intCount + 1
             End If
         Next
Have I made a glaring error somewhere?
Laurie

  07-Nov-2009  12:57
  07-Nov-2009  14:07   
Yes, the problem is in deleting inside a For Each loop. Instead, use a downcounting loop:

   cnt = oResItems.Count
   For i = cnt to 1 Step -1
       Set oAppt = oResItems.Item(i)
       oAppt.Delete
   Next

The problem would exist regardless of the folder. I can't explain why you might not have seen it in your own folder.
  08-Nov-2009  10:19   
That was it - strange though that it always worked on my calendar! I should have thought about it as I had already come across the 'delete from the end' method with removing multiple items from a list box earlier on this project!
Thanks Sue!
    Page [ 1 ]  
Post a new message to this topic.



name        email