1
votes

I need to create custom filters in Outlook to save me from having to manually adjust the filter setting each time, preferably with VBA.

Below is my attempt. I inserted the message box line to check the correct items are being restricted. On running the macro I get a number of message boxes displayed with "1" indicating to me that it is working as expected (message box appears for each 'In Progress' item).

For Each Task_List In CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(13).Items.Restrict("[Status]='In Progress'")
        MsgBox Task_List.Status
        sFilter = "[Status]=Task_List.Status"
Next

However, the tasks in the task folder are not filtered, all the tasks are displayed regardless of criteria.

What am I missing from my code? Or am I completely barking up the wrong tree.

Thanks, and apologies in advance for the simplistic question.

3
More Google searching has revealed it is possible to setup multiple "views" which allow you to filter by various criteriaas_stack

3 Answers

0
votes

Once you manually set up different views you can get to them this way.

Where the view is named for instance "In Progress"

Sub TaskView_InProgress()
    ' No error if the view does not exist
    ' No error if not currently in Tasks folder
    ActiveExplorer.CurrentView = "In Progress"
End Sub
0
votes

This demonstrates how to access the In Progress tasks. Albeit much less helpful than a view if you have many tasks.

Private Sub task_Filter()

' Folders may contain any type of item
Dim myItem As Object

Dim myItems As items
Dim resItems As items

Dim myTaskFolder As Folder

Dim sFilter As String
Dim msgPrompt As String

Set myTaskFolder = Session.GetDefaultFolder(olFolderTasks)

Set myItems = myTaskFolder.items

sFilter = "[Status]='In Progress'"
Set resItems = myItems.Restrict(sFilter)

For Each myItem In resItems
    If myItem.Class = OlTask Then
        myItem.Display
    End If
Next

End Sub
0
votes

This sub worked great for my purpose. I wanted to also input a string in the search field of the task window from excel. So I loaded the string to the clipboard and used send keys to "Ctrl E" (enter search field) then "Ctrl V" paste. This routine turns num lock off. So I added a toggle for that.

Sub btn_GotoTask()

    Set cl = New clsClient
    ' Folders may contain any type of item
    Dim myItem As Object

    Dim myItems As items
    Dim resItems As items

    Dim myTaskFolder As Folder

    Dim sFilter As String
    Dim msgPrompt As String

    On Error GoTo outlookError
    Set myTaskFolder = Session.GetDefaultFolder(olFolderTasks)
    myTaskFolder.Display

    SetClipboard cl.Pol

    'Activate task window
    myTaskFolder.Application.ActiveWindow

    SendKeys "^{e}"
    SendKeys "^{v}"

    SendKeys "{NUMLOCK}"
Exit Sub
outlookError:

MsgBox "Outlook may not be open"

End Sub