1 Attachment(s)
Programmatically Create an Outlook Search Folder
This code will create a search folder in Outlook 2003+. Its convienent when you frequently run the
same search over and over again.
The search folder contents (if any hits are returned) are not actually in the folder but is just a query view result
listing. So if you were to delete the search folder you will not be deleting the contents. But if you view a particular
found item and delete it, then you will be deleting the actual item.
The SCOPE is newly added to 2003 but is originally from Exchange Server.
Enjoy and please post any comments or features you would like to see.
VB Code:
Option Explicit
'Add reference to MS Outlook 11.0 Object Library
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oSearch As Outlook.Search
Dim oInbox As Outlook.MAPIFolder
Dim oSearchFolder As Outlook.MAPIFolder
Dim sFolderPath As String
Dim sScope As String
Dim sFilter As String
On Error Resume Next
Set oApp = New Outlook.Application
Set oInbox = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
sFolderPath = oInbox.FolderPath
'Build a scope string
sScope = "SCOPE ('shallow traversal of " & Chr$(34) & sFolderPath & Chr$(34) & "')"
'Build a filter string (WHERE clause without the WHERE)
sFilter = Chr$(34) & ("urn:schemas:mailheader:subject") & Chr$(34) & " LIKE 'RE:%'"
'Create the Search object by calling AdvancedFind
Set oSearch = oApp.AdvancedSearch(sScope, sFilter, False, "RE Search")
'Save the Search as a Search Folder
Set oSearchFolder = oSearch.Save("RE Search")
MsgBox oSearchFolder.Items.Count & " Items found!"
'oSearchFolder.Delete
End Sub
VB/Outlook Guruâ„¢
:thumb:
Re: Programmatically Create an Outlook Search Folder
Hey Rob,
How can i delete the search folder programmatically???
Is it possible?
I was not able to find any answer to this
Thanks
-SpYeYeS
Re: Programmatically Create an Outlook Search Folder
Its possible (user created) as its nothing more then a mapifolder object with a filter on it.
Just create an object variable of mapifolder type and set it to the search folder desired to be deleted and .Delete it.
Code:
Dim oDelSearch As Outlook.MAPIFolder
Set oDelSearch = oApp.GetNamespace("MAPI").Folders("Your Search Folder Name")
oDelSearch.Delete
Re: Programmatically Create an Outlook Search Folder