Results 1 to 4 of 4

Thread: Programmatically Create an Outlook Search Folder

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Option Explicit
    2. 'Add reference to MS Outlook 11.0 Object Library
    3. Private Sub Command1_Click()
    4.  
    5.     Dim oApp As Outlook.Application
    6.     Dim oSearch As Outlook.Search
    7.     Dim oInbox As Outlook.MAPIFolder
    8.     Dim oSearchFolder As Outlook.MAPIFolder
    9.     Dim sFolderPath As String
    10.     Dim sScope As String
    11.     Dim sFilter As String
    12.    
    13.     On Error Resume Next
    14.     Set oApp = New Outlook.Application
    15.     Set oInbox = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    16.     sFolderPath = oInbox.FolderPath
    17.     'Build a scope string
    18.     sScope = "SCOPE ('shallow traversal of " & Chr$(34) & sFolderPath & Chr$(34) & "')"
    19.     'Build a filter string (WHERE clause without the WHERE)
    20.     sFilter = Chr$(34) & ("urn:schemas:mailheader:subject") & Chr$(34) & " LIKE 'RE:%'"
    21.     'Create the Search object by calling AdvancedFind
    22.     Set oSearch = oApp.AdvancedSearch(sScope, sFilter, False, "RE Search")
    23.     'Save the Search as a Search Folder
    24.     Set oSearchFolder = oSearch.Save("RE Search")
    25.     MsgBox oSearchFolder.Items.Count & " Items found!"
    26.     'oSearchFolder.Delete
    27. End Sub
    VB/Outlook Guru™

    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width