Outlook 2007 VBA Not Working after SP2

B

bfoxall

I originally tried to post this on a similar thread, but it kept
disappearing, so here goes...

I have encountered an issue with some Outlook 2007 vba after upgrading to
SP2. The original code was like this:

Set myOlApp1 = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp1.GetNamespace("MAPI")

Set myTasks = myNameSpace.GetDefaultFolder(olFolderTasks).Items
Set myitems = myTasks.Restrict("[Categories] = XYZ Category")

For Each myItem In myItems
If (myItem.Class = olTask) Then
myItem.Delete
End If
Next

Set myitems = Nothing

After SP2, this code would prematurely break out of the For Each ... Next
loop without deleting all the Task items in the collection.

After debugging and trying to delete the items by referring directly to
their index in the collection, I think I found where the issue is eg:

i = 1
For c = 1 To myItems.Count
If (myitems(i).Class = olTask) Then
myitems(i).Delete
Else
i = i + 1
End If
Next

The problem here is that regardless of the index of the item you attempt to
delete, while it physically deletes the correct item, it deletes the last
item in the myItems collection. Now this code may not have worked correctly
anyway, but debugging through it highlights the disconnect as the myItems
collection becomes out of synch with reality.

I suspect the issue is the same under both examples; both code examples
exhibit the same result regarding which Task items get deleted, and which
remain.

I'd be interested to see if this can be recreated elsewhere under Outlook
2007 SP2. Strangely enough, the .Remove method works fine:

If (myitems(i).Class = olTask) Then
myitems.Remove (i)
Else
i = i + 1
End If

Any help will be appreciated...


Cheers,


Bruce
 

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