These two samples contains the basic code for working with all items in a selected folder or selected items in a folder. The code sample prints the item subject in the Immediate window (Ctrl+6).
Because the code uses generic objects instead of specific objects, it will work with all item types.
Work with all items in any folder
Option Explicit
Public Sub DoSomethingFolder()
Dim objOL As Outlook.Application
Dim objItems As Outlook.Items
Dim objFolder As Outlook.MAPIFolder
Dim obj As Object
Set objOL = Outlook.Application
Set objFolder = objOL.ActiveExplorer.CurrentFolder
Set objItems = objFolder.Items
For Each obj In objItems
With obj
' do whatever
Debug.Print .Subject
End With
Next
Set obj = Nothing
Set objItems = Nothing
Set objFolder = Nothing
Set objOL = Nothing
End Sub
To work with all items in a specific folder, replace
Set objFolder = objOL.ActiveExplorer.CurrentFolder
with GetDefaultFolder and locate the folder (if it's not a default folder).
Set objFolder = Ns.GetDefaultFolder(olFolderCalendar)
Set objFolder = Ns.GetDefaultFolder(olFolderCalendar).Folders("Subfolder")
You'll also need to add these two lines to the macro - Dim Ns should be the first Dim statement, and Set Ns needs to be the first Set statement.
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
See VBA and non-default Outlook Folders for more information.
Work with Selected items in any folder
Option Explicit
Public Sub DoSomethingSelection()
Dim Session As Outlook.NameSpace
Dim currentExplorer As Explorer
Dim Selection As Selection
Dim obj As Object
Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
For Each obj In Selection
With obj
' do whatever
Debug.Print .Subject
End With
Next
Set Session = Nothing
Set currentExplorer = Nothing
Set obj = Nothing
Set Selection = Nothing
End Sub
Leave a Reply
Be the First to Comment!
Hi Diane,
Do you have any suggestions around how to handle the scenario where you want to loop through each object in a selection if one or more items are selected but otherwise loop through each object in the folder if no items are selected? Is there any way to do something along the lines of this pseudocode:
Thanks!
That would be difficult, i'd think... because you'll always have at least 1 selected. You can loop the folder if 1 is selected, or do the selection if more than one is selected.
This little tweak will do the entire folder if 1 is selected, or only the selection if more than one is selected.
Hi Diane,
I have an issue, I am hoping perhaps you or someone here can provide insight.
For a selected email, I assign user defined category from combobox and move the email to a folder. It works fine with emails in my Inbox.
When I do the same for emails in shared inbox, it simply moves it without assigning category. And there is no error message.
Any help appreciated.
Thank you
Does the category exist in the shared mailbox? Are you using code with the custom form? If so, it could be something with the code too.
Hi Diane,
Thank you for this tutorial, we use it to send all drafts in draft folder using a specific email address (SentOnBehalfOfName).
However, the macro will only send half of the drafts in the folder, do you know if there's a fix for this?
thank you very much.
Public Sub DoSomethingFolder()
Dim objOL As Outlook.Application
Dim objItems As Outlook.Items
Dim objFolder As Outlook.MAPIFolder
Dim obj As Object
Set objOL = Outlook.Application
Set objFolder = objOL.ActiveExplorer.CurrentFolder
Set objItems = objFolder.Items
For Each obj In objItems
With obj
.SentOnBehalfOfName = "XXX@gmail.com"
.Send
End With
Next
Set obj = Nothing
Set objItems = Nothing
Set objFolder = Nothing
Set objOL = Nothing
End Sub
Sorry I missed this earlier. I wonder if it is losing the count as messages are sent, much like deleting items will get every other one because the index # changes as you delete. The fix is to count backwards - For i = 1 to objitems.count step -1
I changed it for a specific folder.
Then i get a error:
Fout -2147221233 (8004010f) tijdens uitvoering
De bewerking is mislukt. Kan een object niet vinden
Why? And how to solve it?
what code are you using to identify the folder? The error says 'Can not find an object' - and I'm guessing the object it can't find is the folder.
i have already made code to save attachment from share mail box to sharepoint. but i am getting difficulties in saving attachment from specific date range kindly help
so you want to save only those in a specific range, not the file date?
Two methods: Select them then run a macro (second macro) or set the macro to check the date on each message in the folder.
if you are calculating the date using today's date as the base, you can do something like this:
intDateDiff = DateDiff("d", obj.SentOn, Now)
If intDateDiff > 7 Then ' older than 7 days
' do whatever
end if
Dianne,
Great code. Do you know how to adapt this to modify the calendar folder to delete the subject and add the organizer? We have it set correctly moving forward but after migration all previous reservations show the subject instead of the organizer.
Any help appreciated.
Thank you,
Have a great one
I assume you mean different code than is what on this page... you'll use code similar to .subject = .organizer
if you are using exchange, the new server can be configured to show the organizers name on resource calendars.
Dianne thanks a lot for your code on "Work with all items in any folder". I was able to adapt it to create a list of sender names from 148 email responses saved in an Outlook folder saving me a lot of tedium and fat fingering to create a dg group. I also wanted to look at emails attached to some of the emails in the folder and get those sender names as well. I looked at different objects and attributes but couldn't find it. Is there a way to do this? Thanks in advance.
The emails were attachments on other emails?
you need to check for attachments, save & open them. open attached messages
Hi Diane!
I'm trying to get the size of each folder in the Inbox. That's not hard to do, just takes a lil time.
But, I don't know (and haven't been able to find) how to pull information from non-cached emails. I mean, those that only show after you click "Click here to view more on Microsoft Exchange".
Do you know how to do this, will you help me?
Thanks a lot!
Dimas
What version of Exchange? Do you need to use VBA or can you use powershell?