PDA

View Full Version : Macro to open all emails in Inbox



dalep
09-04-2012, 04:17 PM
Hello,

I'm trying to write a simple script to open all 6,000+ emails in my outlook inbox so that they are all retrieved from an archive.

I had started doing this manually (in batches of 50 emails) but a friend has said I can automate this process with a macro script. I know this must be a simple operation in VB, but I don't know where to start!

This is the process that I would like to learn how to loop through:

1. Take the first email in my inbox
2. Open it (by performing the same action as double-clicking the email sender and subject)
3. Wait for email to be retrieved from the archive and appear in a new message window
4. Close the message window and move onto the next email

Any help would be greatly appreciated. Thanks a lot in advance.

JP2112
09-14-2012, 12:18 PM
Although it sounds like there should be another way to do this, this procedure should do what you need.

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub LoopThruEmails()
Dim i As Long
Dim InboxItems As Outlook.Items
Dim thisEmail As Outlook.mailItem
Set InboxItems = GetItems(GetNS(GetOutlookApp), olFolderInbox)
' assume there are inbox items
For i = 1 To InboxItems.count
If TypeName(InboxItems.item(i)) = "MailItem" Then ' it's an email
Set thisEmail = InboxItems.item(i)
thisEmail.Display
Sleep (5000) ' wait 5 seconds
thisEmail.Close olDiscard
End If
Next i
End Sub
Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End Function
Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function
Function GetItems(olNS As Outlook.NameSpace, folder As OlDefaultFolders) As Outlook.Items
Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function

Rich2877
05-06-2013, 08:11 AM
I read your script with great interest as this looks like something we could use or at least, a modified version of it. We have an email archive system that is dying rapidly and are moving to using the pst files instead.

The problem is that a number of archived emails are in sub-folders from either the root or off the inbox. We need to copy all emails from the OWA to the pst then open everything to unstub all emails.

Actually, here's the real problem - I'm not that hot on scripting but can follow what your script is doing! Is it possible to amend this script to cater for subfolders? We have some people with multi-Gbs of emails so doing this manually is not an option.

Thanks
Rich