Adding Categories Programmatically

O

Odd Spot

I have an add-in that listens for a SelectionChange event on any active
explorer. I then add a category to each MailItem in the selection by
using the following code:

String newCat = "New Category";
Selection s = application.ActiveExplorer().Selection;
foreach( MailItem selectedItem in s ){
String cats = selectedItem.Categories;
if (cats == null || cats.Length == 0)
selectedItem.Categories = newCat;
else if (cats.IndexOf(node.RelativePath) == -1)
selectedItem.Categories += "," + newCat;
}

Once this is done the Categories property shows the new category, but,
on all but the first mail item in the selection, the categories list
accessed from Outlook does not display the new category and a search
folder that is searching for the new category will not find any of the
emails but the first. Furthermore, when Outlook is restarted only the
first email will continue to have the category, even in the Category
property.

Any ideas about what is going on here? Is there a better way to add or
remove a property then via the Categories property?
 
S

Sue Mosher [MVP-Outlook]

I don't see any statement to save the item. Also, you can't assume that
every item in the selection is a MailItem.
 
O

Odd Spot

Sue,

Thanks, Saving the MailItem does the trick.

Also, do you know of any way to create a search folder that retrieves
every message from a specific folder? I know there is an "InFolder"
property, but this property does not contain the full folder path, so
if I have two folders with the same name than the search folder will
retrieve everything from both folders.


Thanks,
Jared


-- Quoted Text --


I don't see any statement to save the item. Also, you can't assume that
every item in the selection is a MailItem.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



- Hide quoted text -
- Show quoted text -
I have an add-in that listens for a SelectionChange event on any active
explorer. I then add a category to each MailItem in the selection by
using the following code:
String newCat = "New Category";
Selection s = application.ActiveExplorer().Selection;
foreach( MailItem selectedItem in s ){
String cats = selectedItem.Categories;
if (cats == null || cats.Length == 0)
selectedItem.Categories = newCat;
else if (cats.IndexOf(node.RelativePath) == -1)
 
S

Sue Mosher [MVP-Outlook]

I imagine it can be done, but why? A search folder that shows all the items
in a folder is identical to the original folder.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
O

Odd Spot

Sue,

Also you mentioned the fact that you can't guarantee that every item in
a Selection object is a MailItem. What is the proper way to check for
this. The getType() method returns an ambiguous com object reference,

Jared



--Quoted Text--

I don't see any statement to save the item. Also, you can't assume that
every item in the selection is a MailItem.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



- Hide quoted text -
- Show quoted text -
I have an add-in that listens for a SelectionChange event on any active
explorer. I then add a category to each MailItem in the selection by
using the following code:
String newCat = "New Category";
Selection s = application.ActiveExplorer().Selection;
foreach( MailItem selectedItem in s ){
String cats = selectedItem.Categories;
if (cats == null || cats.Length == 0)
selectedItem.Categories = newCat;
else if (cats.IndexOf(node.RelativePath) == -1)
 
S

Sue Mosher [MVP-Outlook]

Use the Class property.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
O

Odd Spot

I am trying to create Search Folders that aggregate all of the messages
from a given folder and all of its child folders, but am currently
getting extra messages because of this issue.

Jared

--Quoted Text --



Sue Mosher [MVP-Outlook] Jan 14, 10:26 am

I imagine it can be done, but why? A search folder that shows all the
items
in a folder is identical to the original folder.

--
Thanks, Saving the MailItem does the trick.
Also, do you know of any way to create a search folder that retrieves
every message from a specific folder? I know there is an "InFolder"
property, but this property does not contain the full folder path, so
if I have two folders with the same name than the search folder will
retrieve everything from both folders.

-- Quoted Text --
I don't see any statement to save the item. Also, you can't assume that
every item in the selection is a MailItem.
 
S

Sue Mosher [MVP-Outlook]

That makes more sense. You'd need to use AdvancedSearch to create the
search. Scope should be the path to the top-level folder you want to search,
then use no FIlter and SearchSubFolders set to True. Once the search
completes, use Search.Save to create the search folder.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
O

Odd Spot

Sue,

That makes sense, but unfortunately there are certain situations where
I need to look for a given category as well, and therefore can not
limit the scope to just the top-level folder, as there may be messages
with this category elsewhere.

Sorry to make it more complicated. I should have fully explained the
situation from the get-go.

Thanks for all the help already.

Jared


--Quoted Text--

That makes more sense. You'd need to use AdvancedSearch to create the
search. Scope should be the path to the top-level folder you want to
search,
then use no FIlter and SearchSubFolders set to True. Once the search
completes, use Search.Save to create the search

folder.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators
, Power Users, and Developers

news

:[email protected]...

- Hide quoted text -
- Show quoted text -
 
O

Odd Spot

Sue,

Well I need to create an AdvancedSearch that performs both tasks:

1) Find all mail items from a given folder and its sub-folders.
2) Find any mail items that have a given category regardless of what
folder they are in.

So the scope parameter is not going to help because a message with the
correct category could exist in any folder.

Let me know if I am misunderstanding your suggestion.

Jared



--Quoted Text--

Then why did you say this earlier:

Have you tried this syntax, as documented at
http://msdn.microsoft.com/library/en-us/vbaol10/html/olmthAdvancedSea...

Const strScope As String = "'Inbox', 'Calendar', 'Tasks'"

You'd use a path string for each folder, I think.
--

Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers

"Odd Spot" <[email protected]
wrote in message
news

:[email protected]...

- Hide quoted text -
- Show quoted text -
That makes sense, but unfortunately there are certain situations where
I need to look for a given category as well, and therefore can not
limit the scope to just the top-level folder, as there may be messages
with this category elsewhere.
 
S

Sue Mosher [MVP-Outlook]

Then maybe start the search at the root of the information store?

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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