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 Selection = Nothing Set currentExplorer = Nothing Set obj = Nothing Set Selection = Nothing End Sub
Is there a way to get all of the email items in an Outlook folder regardless if they're on the Exchange server or just in Outlook? The policy where I work won't allow me to change the caching option. I was hoping for a property that I could set or method to pull them all in.
Please, how can I make it go through the mail starting from the most recent mail to the oldest?
In Outlook 14 you loop from newest to oldest and Outlook 16 loops from oldest to newest.
Hi Diane, hope you're doing fine, how do I specify certain links to be opened in an email, for example, if I have an email that has 6 links but I only want to open 3 links out of it, how do I define the pattern so that it only opens those 3 instead of all, let's say http://www.google.com, http://www.gmail.com, and http://www.ymail.com.
you need an if statement -
You use this if you want to skip the url -
If InStr(strURL, "google.com") Then GoTo NextURL
If you want to skip all but certain sites, this should work
If InStr(strURL,"google.com") = 0 Then GoTo NextURL
Hi Diane,
It works only on one, is there a way I can add multiple? for example, when I say
If InStr(strURL,"google.com") = 0 Then GoTo NextURL it opens Google.com however I still have Ymail & Gmail in the email which I want to open may I know how do I add them?
If InStr(strURL,"google.com") = 0 Then GoTo NextURL
If InStr(strURL,"ymail.com") = 0 Then GoTo NextURL
If InStr(strURL,"gmail.com") = 0 Then GoTo NextURL
I tried the above still it only opens "Google" but doesn't open "ymail & gmail"
I'm sorry if this is a dumb question, I'm a novice, your help is highly appreciated.
Logic error. :) Goto NextURL skips the other two.
This should work -
If InStr(strURL,"google.com") = 0 Then
If InStr(strURL,"ymail.com") = 0 Then
If InStr(strURL,"gmail.com") = 0 Then GoTo NextURL
End if
End if
Thanks a Million! You're a life saver!
Hi Diane, When I used the code it only opens only the links on the first email even though the. Global value is set to True, there are 12 emails in the folder and it always opens the links on the first email, how do I get it to open all, your help is highly appreciated.
global opens all links in the selected message. If you want to open links in the selected message, you need to use code that loops the selection.
https://www.slipstick.com/developer/code-samples/open-hyperlinks-email-message/#selected
Thanks a Million! one follow-up question, say I've ~10 hyperlinks in which I only want to open 3 specific ones, just for example say those are Ymail, Gmail & Google how do I modify the ".Pattern" code to let the script know that I only want to open the above 3, I tried different methods but no go, as always your help is highly appreciated!
Hi Diane,
Thanks for the tutorial, I hope you are doing well, I have one question to you, can I move the mails (only those mails that have category blank) from a folder to another folder at once like copy and paste not through loop, move bulk mails at once, is this possible? if yes? please help me.
Like if we do manually then will go to that folder then apply filter the mails then select those filtered mails and drage those mails to another folder, at once we moved all the mails to difference folder.
Please take good care of yourself and your family.
Thanks
Kashif
Diane - you amaze me. I'm relatively new and I always end up on your site for solutions.
I want to display collection of MailItems I've captured in my current Explorer window. Basically, I want to do the equivalent of an instant search but programmatically...
I can't figure out how to get teh collection to display ... :/
set olExplorer = ActiveExplorer
olExplorer.Display(itmEmails)
That's clearly not right.
Can you push me in the right direction?
Hi! Can someone help me with a macro wherein I am trying to extract information from the emails, but not from all the emails. The macro needs to filter the emails based on the subject of the email and then extract information from the same?
This will get values from the message - as long as nothing in the other messages match, it could run on all messages in the folder.
Use RegEx to extract text from an Outlook email message (slipstick.com)
Otherwise you need to either use an if statement to check the subject -
If instr(1, item.subject, "phrase" > 0 then
' extract
end if
or use a find or restrict filter then run the macro on the filtered versions - this would be faster if there were "a ton" of messages and only a few needed to be processed.
So if I wanted to select only the items visible in a filtered view of the folder, would it be:
Set currentExplorer = objOL.ActiveExplorer
Set Selection = currentExplorer.SelectAllItems
?
Thx, you're awesome.
It would still be the same as with a selection -
For Each obj In Selection
With obj
' do whatever
Debug.Print .Subject
End With
Next