Outlook loop items only applies to first item

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi All

Snippet from my Outlook VBA:
VBA Code:
If .optCategorize.Value Then
    For Each varItem In ActiveExplorer.Selection
        If TypeName(varItem) = "MailItem" Then
            'Debug.Print varItem.Subject, strCats
            varItem.Categories = strCats
        End If
    Next varItem
End If

If I select multiple mail items, while it does appear to loop all items (the debug print correctly reveals each item, and the string value is the same for each item), it doesn't apply strCats to the mail item categories property for any but the first item.

Any idea why?

Regards
Jon
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Easy one Jon :)

You need to save each mail item since only one of them is active in the selection. I know, it doesn't make sense but it is how it is.

VBA Code:
    For Each varItem In ActiveExplorer.Selection
        If TypeName(varItem) = "MailItem" Then
            'Debug.Print varItem.Subject, strCats
            varItem.Categories = strCats
            varItem.Save
        End If
    Next varItem
 
Upvote 0
Solution

Forum statistics

Threads
1,223,317
Messages
6,171,422
Members
452,402
Latest member
siduslevis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top