Create an `Items` collection containing references to already existing `Item`s

最后都变了- 提交于 2021-01-28 06:08:10

问题


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


回答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




回答2:


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.



来源:https://stackoverflow.com/questions/29071174/create-an-items-collection-containing-references-to-already-existing-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!