Tuesday, April 6, 2010

A macro in Office Outlook to export mails

There’s no honour in working hard inefficiently. Only disgrace.

I read this somewhere on net. So I thought, why not make my daily chores like sorting and clearing my inbox more automated and less time consuming. Hence I wrote a code snippet that saved me a lot of time and effort. And moreover it made me happy everytime i ran the script. ;)

What the script does : It send all the mails in the folder "Nice" under "Inbox" to an email id of your choice and then deletes them from the folder "Nice".



Steps to set up this in your Outlook
1.) Goto Tools->Macros->Macros or Alt+F8
2.)Type Macro name as ExportToGmail and click on Create
3.)Microsoft Visual Basic Editor will open up.
4.)Copy paste the below code in the appopriate place for the Sub ExportToGmail()

==============================================

Sub ExportToGmail()


Dim objOutlook As New Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objInbox As MAPIFolder
Dim objConvHist As MAPIFolder
Dim objMail As MailItem
Dim myFolder As MAPIFolder


'Get the MAPI reference
Set objNameSpace = objOutlook.GetNamespace("MAPI")


'Pick up the Inbox
Set objInbox = objNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = objInbox.Folders("Nice")


For Each Item In myFolder.Items
Set mailToSend = objInbox.Items.Add


With mailToSend
.Subject = Item.Subject
.Body = Item.Body
.To = "ABC@XYZ.com"
.Send
End With


Item.Delete
Next Item


MsgBox "Your mails from folder NICE are exported to Gmail", vbInformation, Oops
End Sub

===============================================
5. Change the (.To =) from ABC@XYZ.com to your mail id. and save the code. (Mail Id should be in double quotes)
6. Create folder called "Nice" below inbox in your outlook. If you want to change the name of the folder, change the entry in the code respectively.
7. Move all the emails that you want to export to the folder "Nice" .
8. Now go to Goto Tools->Macros->Macros or press Alt+F8
9. Click on the ExportToGmail() Macro in the list and click Run

If you want to use this macro regularly just let me know. I will explain how to create an icon on Outlook for the same. So that you can export all the mails in the folder just by a click of the mouse.

No comments: