Sunday, 23 January 2011


ill now, we have covered the basics of automating MS Outlook as well as overview of Outlook Object Model. In this article, you’ll see how you can access the Outlook folders and work with them. You would see how to find specific folder by name, how to create a new folder in outlook, how to delete an existing folder and how to move/copy folders to a given destination.
To access Outlook folders, you would need to use the MAPIFolder class which contains the methods and properties needed to access, create and customize folders. MAPIFolder provides 16 default objects using which you can access different Outlook folders such as e-mail messages, tasks, contact items etc. Let us some examples on how you can access outlook folders and work with them.


Sample Code 1: Display total number of folders and name of each folder inside Inbox Folder.Let’s see the code that displays the number and names of folders inside Inbox folder. The figure below the code snippet shows the snapshot of Outlook Inbox folder and the output value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Dim iCount, sFolderNames
sFolderNames = ""
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
'Create reference to Inbox Folder
Set oInbox = objNamespace.GetDefaultFolder(6)
'Find list of all folders within Inbox Folder
Set oAllFolders = oInbox.Folders
iCount = oAllFolders.Count
sFolderNames = "Total Folders = " & iCount & vbcrlf & vbcrlf
'Get the names of the folders
For i = 1 to iCount
    sFolderNames = sFolderNames & "Folder " & i & " -> " & oAllFolders(i).Name & vbcrlf
Next
'Display folder count and folder names in msgbox
msgbox sFolderNames
Working with Outlook Folders
Working with Outlook Folders


Sample Code 2: Add and Delete folders in Outlook. In this example, we’ll add a new folder (named Folder1) and delete an already existing folder (Test3) from Outlook Inbox.
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 addFolderName, delFolderName
addFolderName = "Folder1"
delFolderName = "Test3"
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
'Create reference to Inbox Folder
Set oInbox = objNamespace.GetDefaultFolder(6)
'Add new folder in Outlook Inbox
oInbox.Folders.Add addFolderName
'Find list of all folders within Inbox
Set oAllFolders = oInbox.Folders
iCount = oAllFolders.Count
'Delete folder from Outlook Inbox
For i = 1 to iCount
 If oAllFolders(i).Name = delFolderName Then
  oAllFolders(i).Delete
  Exit For
 End If
Next
Add & Delete Folders from Outlook
Add & Delete Folders from Outlook


Sample Code 3: Copy and Move folders from one location to another. In this example, we’ll copy and move folders from Inbox to Junk Folder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
'Create reference to Inbox & JunkMail Folders
Set oInbox = objNamespace.GetDefaultFolder(6)
Set oJunk = objNamespace.GetDefaultFolder(23)
'Find list of all folders within Inbox
Set oAllFolders = oInbox.Folders
iCount = oAllFolders.Count
'Delete folder from Outlook Inbox
For i = 1 to iCount
    If oAllFolders(i).Name = "Copy Folder" Then
      'Copy folder from Inbox to Junk Folder
      oAllFolders(i).CopyTo(oJunk)
    ElseIf oAllFolders(i).Name = "Move Folder" Then
      'Move folder from Inbox to Junk Folder
      oAllFolders(i).MoveTo(oJunk)
    End If
Next
Copy and Move Folders in Outlook
Copy and Move Folders in Outlook


In all the above examples, we have worked mostly with the Inbox folder (which was represented by integer value 6). If you want to work with any other folder in Outlook (such as Drafts, Sent Items, etc), all you need to do is use the enumeration value associated with that particular folder. For your reference, we have listed down all the 16 default outlook folder types with their enumerations.
Name
Value
Description
olFolderCalendar9The Calendar folder.
olFolderConflicts19The Conflicts folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderContacts10The Contacts folder.
olFolderDeletedItems3The Deleted Items folder.
olFolderDrafts16The Drafts folder.
olFolderInbox6The Inbox folder.
olFolderJournal11The Journal folder.
olFolderJunk23The Junk E-Mail folder.
olFolderLocalFailures21The Local Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderManagedEmail29The top-level folder in the Managed Folders group. For more information on Managed Folders, see Help in Microsoft Outlook. Only available for an Exchange account.
olFolderNotes12The Notes folder.
olFolderOutbox4The Outbox folder.
olFolderSentMail5The Sent Mail folder.
olFolderServerFailures22The Server Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderSyncIssues20The Sync Issues folder. Only available for an Exchange account.
olFolderTasks13The Tasks folder.
olFolderToDo28The To Do folder.
olPublicFoldersAllPublicFolders18The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account.
olFolderRssFeeds25The RSS Feeds folder.

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