Sunday, 23 January 2011


In this article, you would see how to access emails in outlook, how to send emails using outlook and how to access different properties of an email item. To access email items, you can use MailItem class which contains the methods and properties to work with email items. Let’s directly jump onto the examples -


Sample Code 1: Sending emails using Outlook. This topic has already been covered in a previous article. You can read how to send emails using Outlook (You can also read how to send emails using GMail and Yahoo).


Sample Code 2: Access different properties of a mail item. In this example, you’ll see how to access the mails in an outlook folder (eg. inbox folder) and find out various properties of a particular mail item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Dim olFolderInbox, iTotalMails, sSubject
olFolderInbox = 6 : sSubject = ""
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
'Create reference to Inbox Folder
Set oInbox = objNamespace.GetDefaultFolder(olFolderInbox)
'Find all items in the Inbox Folder
Set oAllMails = oInbox.Items
'Find out properties of the mail item
sSubject = sSubject & "To -> " & oAllMails(1).To & vbCrLf
sSubject = sSubject & "CC -> " & oAllMails(1).CC & vbCrLf
sSubject = sSubject & "BCC -> " & oAllMails(1).BCC & vbCrLf
sSubject = sSubject & "Subject -> " & oAllMails(1).Subject & vbCrLf
sSubject = sSubject & "Body -> " & oAllMails(1).Body & vbCrLf
sSubject = sSubject & "Creation Time -> " & oAllMails(1).CreationTime & vbCrLf
sSubject = sSubject & "Is Marked Important -> " & oAllMails(1).Importance & vbCrLf
sSubject = sSubject & "Received at -> " & oAllMails(1).ReceivedTime & vbCrLf
'Display the result
msgbox sSubject
Outlook Email Properties
Outlook Email Properties


Sample Code 3: Find out the list of all unread emails in Outlook Inbox. This example loops through all the emails in Inbox folder and reports out the unread email items.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Dim olFolderInbox, iTotalMails, sSubject
olFolderInbox = 6 : sSubject = ""
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
'Create reference to Inbox Folder
Set oInbox = objNamespace.GetDefaultFolder(olFolderInbox)
'Find all items in the Inbox Folder
Set oAllMails = oInbox.Items
iTotalMails = oAllMails.Count
'Loop through the mail items
For i=1 to iTotalMails
  'Check if the mail is UnRead or not
  If oAllMails(i).UnRead = True Then
     sSubject = sSubject & oAllMails(i).Subject & vbCrLf
  End If
Next
'Display the results
msgbox sSubject
List of Unread Emails in Outlook Inbox
List of Unread Emails in Outlook Inbox
This article just lists few of the important functions of outlook mail items. You can refer Outlook Object Model to find out more about the different properties and methods that can be used with outlook mail items.

0 comments:

Post a Comment

TrainingHUB. Powered by Blogger.

Total Pageviews

THE BEST QTP TRAINING INSTITUTE IN HYDERABAD

QTP Training in hyderabad

Sql Tutorial

Popular Posts

Our Facebook Page

TrainingHUB

Followers