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 objOL As Outlook.Application Dim currentExplorer As Explorer Dim Selection As Selection Dim obj As Object Set objOL = Outlook.Application Set currentExplorer = objOL.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
Hi Diane,
I have earlier designed a macro to send emails by attaching files and send to specific emails.
Is it possible to send another reminder email by using Outlook vba if the recipient never replies to my 1st email?
Diane,
I have this code that runs when i close outlook, it moves all of my deleted messages to a file. It stopped working with a type mismatch error on this line "et objTrash = Outlook.Application.Session.GetDefaultFolder(olFolderDeletedItems).Items"
Can you please help?
Thank you,
Jim
it quit following an update? Try using
Application.Session.GetDefaultFolder(olFolderDeletedItems).Items
I've had issues with some code failing after updates and simple tweaks that really changed nothing fixed them.
Is there any documentation as to what type of items could exist in a mail folder? Or, at least, that any such items must share some common properties e.g. Categories. For example, MailItem, MeetingItem and ReportItem all have a Subject property, but if your For-Each hits some other type of item that doesn't have it then Debug.Print would throw an error.
I'm not aware of any specific documentation that lists it - there is documentation that lists the fields for each item type.
These links are for an older version but still apply to current versions.
for example, mailitem and reportitem is here:
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa210946(v=office.11)
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa211038(v=office.11)
Hello, was linked here from: https://www.slipstick.com/developer/code-samples/macro-resend-message/?fbclid=IwAR29t9kfQrcG922CkbgQN9m77XVPUBsOc8yKsylxHwN7CTfgUel4cql08OY
I used the following code to resend selected messages and discard the original on Outlook 2010, but it doesn't work correctly on 2016:
Sub BatchResendEmails()
Dim objSelection As Outlook.Selection
Dim objMail As Outlook.MailItem
Dim objInspector As Outlook.Inspector
Dim objResendMail As Outlook.MailItem
Set objSelection = Application.ActiveExplorer.Selection
If Not (objSelection Is Nothing) Then
On Error Resume Next
For Each objMail In objSelection
objMail.Display
Set objInspector = myItem.GetInspector
'Resend message
objInspector.CommandBars.ExecuteMso ("ResendThisMessage")
Set objResendMail = Application.ActiveInspector.CurrentItem
'You can change the email details as per your needs
With objResendMail
.Subject = "" + objMail.Subject
.Send
End With
objMail.Close olDiscard
Next
End If
End Sub
I have a rule to add a category to emails sent with specific text in the subject.
I then filter my sent items for that category, and use the above macro to select emails from the day before and resend them.
As mentioned this above macro doesn't work as intended on outlook 2016, and hoping for help, as I'm not personally that will versed.
Do you receive any error messages? Do you have macro security set to low? The code should work in outlook 2016.
Hello, sorry for the delay I've been off work.
I don't get any error messages, and my macro settings are set to check per session if I want to use it. When I select emails to batch resend that don't have a category, they seem to go fine, but only with a small group. I sometimes need to batch resend about 60 tagged emails, and I will get the sent messages in my inbox, but no new email is produced in my sent items.
The macro is working here, but I only tested it with 3 messages. Is it only failing with a large # of resends?
Hello, it seems to fail with either large volume, or if I have a category set to the email.
Hello, might add, that I am also on windows 10.
My colleague uses the same macro with outlook 2016 but on windows 7.
Hello, sorry i thought I replied to this already. I do not get any error message and my security settings allow macro.
I have read receipts for my emails, they all come through to my inbox, but the emails are no where to be seen in the sent items.
Hi Diane,
Is there a way to assign the selected items or a selection any name or value, so Outlook remembers it and the same selection can be recalled if the outlook folder changes to another folder and back to the original one again?
I have tried using your above example of For Each obj In Selection and just a With Selection
commands, but neither seem to reselect the emails I had originally selected, once I go out of the folder and return to it again.
Thank you for your suggestions.
Reuben
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