site
stats
By Anish Pillai Anish Pillai Posted under Advanced Concepts | QTP Concepts

Part 3: Automating Outlook using QTP | Working with Outlook Folders

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Till 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.

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.

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

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
olFolderCalendar 9 The Calendar folder.
olFolderConflicts 19 The Conflicts folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderContacts 10 The Contacts folder.
olFolderDeletedItems 3 The Deleted Items folder.
olFolderDrafts 16 The Drafts folder.
olFolderInbox 6 The Inbox folder.
olFolderJournal 11 The Journal folder.
olFolderJunk 23 The Junk E-Mail folder.
olFolderLocalFailures 21 The Local Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderManagedEmail 29 The 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.
olFolderNotes 12 The Notes folder.
olFolderOutbox 4 The Outbox folder.
olFolderSentMail 5 The Sent Mail folder.
olFolderServerFailures 22 The Server Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account.
olFolderSyncIssues 20 The Sync Issues folder. Only available for an Exchange account.
olFolderTasks 13 The Tasks folder.
olFolderToDo 28 The To Do folder.
olPublicFoldersAllPublicFolders 18 The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account.
olFolderRssFeeds 25 The RSS Feeds folder.


If you feel that we have missed out any important concept, or if you want any other topic to be covered here, please leave a comment or just drop in a mail to us. We’ll be more than happy to work on it.

If you enjoyed this article, you can join our blog to get new posts delivered directly in your inbox.


0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Praveen

    how to access the mails form sub folders(inbox,folder under inbox)

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×