Problems with .Restrict method..... 
Author Message
 Problems with .Restrict method.....

Al,

I suspect that your problem is a consequence of modifying members of the collection in a way that
changes their membership in the collection while the collection is being enumerated.

I've seen the same sort of problem in scripts that do a For Each enumeration of a FileSystemObject
Folder.Files collection to rename the files.  Files that were renamed reappeared later in the
enumeration under their new name!

Here's what I would try - build an independent array of object references and enumerate the array
instead.  That's what solved the Folder.Files renaming problem.

===============================
Note that this is untested "air code" ;-)...
===============================

Set objRestrictItems = objMessages.Restrict(sFilter)

ReDim arRestrictItems(objRestrictItems.Count-1)

For n = 0 To UBound(arItems)
  '==> Items collection is 1 based
  Set arItems = objRestrictItems.Item(n+1)
Next

For Each objMessage In arRestrictItems
  sOldCategories = objMessage.Categories
  sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
  objMessage.Categories = sCategories
  objMessage.Save
Next

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:

> I need to change the category on a subset of messages in a public folder. I
> create a filter:

> Set objMessages = objFolder.items
> Set objRestrictItems = objMessages.Restrict(sFilter)

> If objRestrictItems.Count > 0 Then
> ' Remove category from each item
>  For Each objMessage In objRestrictItems
>   sOldCategories = objMessage.Categories
>   sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
>   objMessage.Categories = sCategories
>   objMessage.Save
>  Next
> End If

> The objRestrictItems has the correct number of records in it after the
> .Restrict method is used. BUT

> The subsequent code that you can see ALWAYS processes N-1 records! ie if the
> restrict clause results in objRestrictitems.count = 3 then only 2 records
> get processed. If .count = 4 then 3 get processed. The .count value
> decrements with each pass through the for each....next loop. I assume that
> this is the correct behaviour (ie if 3 records are initially selected, after
> processing the first one .count = 2); because as each item is saved it is no
> longer within the scope of the filter so the number should change.

> However to code drops out of the bottom of the For...Next loop when the
> count is 1 - > so it doesnt process the last item in the filter.

> I have fudged the issue by modifying the code as follows:

> Do While objRestrictItems.Count > 0
>  Set objRestrictItems = objMessages.Restrict(sFilter)
>  ' Remove category from each item
>  For Each objMessage In objRestrictItems
>   sOldCategories = objMessage.Categories
>   sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
>   objMessage.Categories = sCategories
>   objMessage.Save
>  Next
> Loop

> So, I have added a loop that re-creates the filter for the last message in
> the category! Its clunky but it works as expected. Why doesnt the first bit
> of code work?

> What is happening to stop the code processing the last member of the filter?

> ============================================================================

> Al Blake
> IT Manager, SPC Noumea

> --
> Al Blake
> IT Manager
> SPC, Noumea
> New Caledonia



Thu, 08 May 2003 03:00:00 GMT  
 Problems with .Restrict method.....

Helps if you use the correct array name everywhere ;-)...

===============================
Note that this is *STILL* untested "air code" ;-)...
===============================

Set objRestrictItems = objMessages.Restrict(sFilter)

ReDim arRestrictItems(objRestrictItems.Count-1)

For n = 0 To UBound(arRestrictItems)
  '==> Items collection is 1 based
  Set arRestrictItems= objRestrictItems.Item(n+1)
Next

For Each objMessage In arRestrictItems
  sOldCategories = objMessage.Categories
  sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
  objMessage.Categories = sCategories
  objMessage.Save
Next

--
Michael Harris
Microsoft.MVP.Scripting
--

Quote:

> Al,

> I suspect that your problem is a consequence of modifying members of the collection in a way that
> changes their membership in the collection while the collection is being enumerated.

> I've seen the same sort of problem in scripts that do a For Each enumeration of a FileSystemObject
> Folder.Files collection to rename the files.  Files that were renamed reappeared later in the
> enumeration under their new name!

> Here's what I would try - build an independent array of object references and enumerate the array
> instead.  That's what solved the Folder.Files renaming problem.

> ===============================
> Note that this is untested "air code" ;-)...
> ===============================

> Set objRestrictItems = objMessages.Restrict(sFilter)

> ReDim arRestrictItems(objRestrictItems.Count-1)

> For n = 0 To UBound(arItems)
>   '==> Items collection is 1 based
>   Set arItems = objRestrictItems.Item(n+1)
> Next

> For Each objMessage In arRestrictItems
>   sOldCategories = objMessage.Categories
>   sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
>   objMessage.Categories = sCategories
>   objMessage.Save
> Next

> --
> Michael Harris
> Microsoft.MVP.Scripting
> --

> > I need to change the category on a subset of messages in a public folder. I
> > create a filter:

> > Set objMessages = objFolder.items
> > Set objRestrictItems = objMessages.Restrict(sFilter)

> > If objRestrictItems.Count > 0 Then
> > ' Remove category from each item
> >  For Each objMessage In objRestrictItems
> >   sOldCategories = objMessage.Categories
> >   sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
> >   objMessage.Categories = sCategories
> >   objMessage.Save
> >  Next
> > End If

> > The objRestrictItems has the correct number of records in it after the
> > .Restrict method is used. BUT

> > The subsequent code that you can see ALWAYS processes N-1 records! ie if the
> > restrict clause results in objRestrictitems.count = 3 then only 2 records
> > get processed. If .count = 4 then 3 get processed. The .count value
> > decrements with each pass through the for each....next loop. I assume that
> > this is the correct behaviour (ie if 3 records are initially selected, after
> > processing the first one .count = 2); because as each item is saved it is no
> > longer within the scope of the filter so the number should change.

> > However to code drops out of the bottom of the For...Next loop when the
> > count is 1 - > so it doesnt process the last item in the filter.

> > I have fudged the issue by modifying the code as follows:

> > Do While objRestrictItems.Count > 0
> >  Set objRestrictItems = objMessages.Restrict(sFilter)
> >  ' Remove category from each item
> >  For Each objMessage In objRestrictItems
> >   sOldCategories = objMessage.Categories
> >   sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
> >   objMessage.Categories = sCategories
> >   objMessage.Save
> >  Next
> > Loop

> > So, I have added a loop that re-creates the filter for the last message in
> > the category! Its clunky but it works as expected. Why doesnt the first bit
> > of code work?

> > What is happening to stop the code processing the last member of the filter?

> > ============================================================================

> > Al Blake
> > IT Manager, SPC Noumea

> > --
> > Al Blake
> > IT Manager
> > SPC, Noumea
> > New Caledonia



Thu, 08 May 2003 03:00:00 GMT  
 Problems with .Restrict method.....

I need to change the category on a subset of messages in a public folder. I
create a filter:

Set objMessages = objFolder.items
Set objRestrictItems = objMessages.Restrict(sFilter)

If objRestrictItems.Count > 0 Then
' Remove category from each item
 For Each objMessage In objRestrictItems
  sOldCategories = objMessage.Categories
  sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
  objMessage.Categories = sCategories
  objMessage.Save
 Next
End If

The objRestrictItems has the correct number of records in it after the
.Restrict method is used. BUT

The subsequent code that you can see ALWAYS processes N-1 records! ie if the
restrict clause results in objRestrictitems.count = 3 then only 2 records
get processed. If .count = 4 then 3 get processed. The .count value
decrements with each pass through the for each....next loop. I assume that
this is the correct behaviour (ie if 3 records are initially selected, after
processing the first one .count = 2); because as each item is saved it is no
longer within the scope of the filter so the number should change.

However to code drops out of the bottom of the For...Next loop when the
count is 1 - > so it doesnt process the last item in the filter.

I have fudged the issue by modifying the code as follows:

Do While objRestrictItems.Count > 0
 Set objRestrictItems = objMessages.Restrict(sFilter)
 ' Remove category from each item
 For Each objMessage In objRestrictItems
  sOldCategories = objMessage.Categories
  sCategories = Replace(sOldCategories, sOldCategory, sNewCategory)
  objMessage.Categories = sCategories
  objMessage.Save
 Next
Loop

So, I have added a loop that re-creates the filter for the last message in
the category! Its clunky but it works as expected. Why doesnt the first bit
of code work?

What is happening to stop the code processing the last member of the filter?

============================================================================

Al Blake
IT Manager, SPC Noumea

--
Al Blake
IT Manager
SPC, Noumea
New Caledonia



Fri, 09 May 2003 08:21:14 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem with Restrict-method for mailitems

2. strange problems with restrict method on calendar userproperties

3. BUG: Problem using the Restrict method in Office 2000 VBA code

4. Restrict method problem

5. VBScript (Outlook) - "Restrict" method

6. Using like conditions with restrict method

7. Restrict method parsing error

8. using Restrict items method

9. Creating Items collection using Find method instead of Restrict

10. Filter for RESTRICT method

11. Restrict method

12. Restrict Method with Appointment Items

 

 
Powered by phpBB® Forum Software