1

I mean to create an Items collection, and add to it several already existing Items. For instance, if I have two references to MailItems, I want to set an Items collection containing those two Items.

It would be something like

' ...
' Code that assigns references to olMail1 and olMail2, of type Outlook.MailItem
' ...
Dim MyItems As Outlook.Items
' Assign with Set / create the object
MyItems.Add olMail1
MyItems.Add olMail2

' Code that can use MyItems(1) to get a reference to olMail1

How can that be done?

Things to clarify are:

  1. How to setup the new collection.
  2. How to add items. Documentation on Items.Add seems to indicate that it is used for adding newly created objects, not references to existing Items.

I would later iterate through that collection, for instance. I would also apply Find or Restrict; this allows for applying the methods on a much smaller collection than a whole Folder.

PS: I cannot get an Items collection even from Application.ActiveExplorer.Selection (i.e., without need for creating the collection and add Items one by one). This would be good for a starter.


Background

I mean to find what Items have a sender matching a given string. The aspects that perhaps make my case somewhat more complex than a "base case" are:

  1. I mean to apply the filter only on a selected group of items. E.g., only on the Items that are selected in the Inbox index.
  2. I want to do partial matching. At this point I do not need regular expressions, or even full use of wildcards *?. But at least partial matching as in InStr.
  3. I mean to have a specific Function for the minimal unit: testing one Item, for a single condition. Then loop through all target Items, and all conditions.

I conceived 3 approaches:

  1. Use Rules.
  2. Use Filter or Restrict. These do not accept wildcards (in principle?).
  3. "Manually" check conditions, with InStr, e.g.

Each of the aspects above may bear some complexity for one or more of the approaches.

At this point, I was exploring approach 2. I have a reference to a single Item, and I found how to apply a Filter with a matching condition (see http://www.outlookcode.com/news.aspx?id=30 , http://blogs.msdn.com/b/andrewdelin/archive/2005/05/11/416312.aspx , and the non-accepted answer of VBA Search in Outlook). But to apply the Filter, I need an Items collection, containing my single item.

I have something working with approach 3 (as suggested in the accepted answer of VBA Search in Outlook).

Related links

Identify MailItems satisfying a Rule

Community
  • 1
  • 1

2 Answers2

1

You can just use a regular collection:

Dim myItems As Collection
Set myItems = New Collection
myItems.Add olMail1
myItems.Add olMail2

Now if you're looking to restrict the type of objects than can be contained by myItems, then it becomes a bit more complicated, but here's a way to do it:

Restrict type in a Collection inside a class module

Community
  • 1
  • 1
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
1

I'd suggest starting from the Getting Started with VBA in Outlook 2010 article in MSDN.

An instance of the Items class can't be created in the code. It is asociated and belongs to any folder. You can create a folder to get a new Items instance.

You can use the Copy method of Outlook items to create another instance of the object. Then the Move method can be used to move the item to another Items collection (folder).

1.I mean to apply the filter only on a selected group of items. E.g., only on the Items that are selected in the Inbox index.

You need to iterate over all selected items instead. The Find/FindNext and Restrict methods belong to the Items class only. So, you can apply them to the Folder items only.

2.I want to do partial matching. At this point I do not need regular expressions, or even full use of wildcards *?. But at least partial matching as in InStr.

See Filtering Items Using a String Comparison. You can use the ci_startswith or ci_phrasematch operators.

3.I mean to have a specific Function for the minimal unit: testing one Item, for a single condition. Then loop through all target Items, and all conditions.

Take a look at the Filtering Items section in MSDN which describes the general rules for specifying properties in filters that are supported by various objects in Outlook.

The Filter method of the View class is applied to the Outlook view only. The Items property will return the full list of items.

It would be better if you specify the final goal, not possible way to solve the problem which is not clear for us.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for a detailed answer. I will have to read and test a bit prior to posting feedback. As for *the final goal*, please see the beginning of **Background** in the OP: *I mean to find what Items have a sender matching a given string*. I wanted to explore options, so it is easy for expanding later the functionality (different conditions/actions). – sancho.s ReinstateMonicaCellio Mar 16 '15 at 16:14
  • `Rules` seemed to have two cons: 1) they seem to act only on entire `Folder`s, 2) it appears to be not possible to check which `Item`s satisfy a condition prior to executing the `Action` (unless one does the job manually, thus not taking advantage of the filter itself). – sancho.s ReinstateMonicaCellio Mar 16 '15 at 16:14
  • I guess your answer to my specific question is "You cannot do that". Actually none of the two required and enumerated actions would be possible. Is this correct? – sancho.s ReinstateMonicaCellio Mar 16 '15 at 21:08
  • "*You need to iterate over all selected items instead*": But would I be able to use `Find` in this case? Could you show specific code (I cannot figure this out)? – sancho.s ReinstateMonicaCellio Mar 17 '15 at 11:17
  • "*... use the **ci_startswith**...*": I had found that option. But I also found that [... if you use the query keywords **ci_phrasematch** or **ci_startswith** in the filter, you will get an error](https://msdn.microsoft.com/EN-US/library/office/ff863965.aspx). Is this contradictory? – sancho.s ReinstateMonicaCellio Mar 17 '15 at 11:19
  • "*Take a look at...*": Again, I do not know if that can be applied to a single Item. Can it? – sancho.s ReinstateMonicaCellio Mar 17 '15 at 11:19