The View object itself is simple but it has one very powerful property. This property is called XML, and it contains an xml definition of a folder view. Today we are going to look at the capabilities of the <filter> element. Using this element you can set or remove a filter for folder items programmatically.
In this sample, we filter Outlook items by one of the two fields – Subject or Body. In real projects you can use other fields as well as other conditions.
Well, let’s go. To set a filter you need to add the <filter> xml element (if it was not added before) to the <view> xml element and call the Apply method:
Private Sub btnSetFilter_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSetFilter.Click
Dim sFilter As String
Dim CurrentExplorer As Outlook.Explorer = Nothing
Dim CurrentView As Outlook.View = Nothing
Dim CurrentXML As XmlDocument = New XmlDocument
Dim CurrentFilterNodes, CurrentViewNodes As XmlNodeList
Dim CurrentFilterNode, CurrentParentNode As XmlNode
If TextBox1.Text.Length > 0 Then
If rbtSubject.Checked Then
sFilter = "urn:schemas:httpmail:subject LIKE '%" + _
TextBox1.Text.Trim + "%'"
Else
sFilter = "urn:schemas:httpmail:textdescription LIKE '%" + _
TextBox1.Text.Trim + "%'"
End If
CurrentExplorer = TryCast(ExplorerObj, Outlook.Explorer)
If (CurrentExplorer IsNot Nothing) Then
CurrentView = CurrentExplorer.CurrentView
If (CurrentView IsNot Nothing) Then
Try
CurrentXML.LoadXml(CurrentView.XML)
CurrentFilterNodes = _
CurrentXML.GetElementsByTagName("filter")
If CurrentFilterNodes.Count > 0 Then
For y As Integer = 0 _
To CurrentFilterNodes.Count - 1
CurrentFilterNode = CurrentFilterNodes(y)
If CurrentFilterNode.HasChildNodes Then
For i As Integer = _
CurrentFilterNode.ChildNodes.Count - 1 _
To 0 Step -1
CurrentFilterNode.RemoveChild( _
CurrentFilterNode.ChildNodes(i))
Next
End If
Next
CurrentFilterNode = CurrentFilterNodes(0)
CurrentFilterNode.AppendChild( _
CurrentXML.CreateTextNode(sFilter))
Else
CurrentViewNodes = CurrentXML. _
GetElementsByTagName("view")
If CurrentViewNodes IsNot Nothing Then
CurrentParentNode = CurrentViewNodes(0)
CurrentFilterNode = CurrentXML. _
CreateElement("filter")
CurrentParentNode.AppendChild( _
CurrentFilterNode)
CurrentFilterNode.AppendChild( _
CurrentXML.CreateTextNode(sFilter))
End If
End If
CurrentView.XML = CurrentXML.InnerXml
CurrentView.Apply()
Finally
Marshal.ReleaseComObject(CurrentView)
End Try
End If
End If
End If
End Sub
To remove a filter you just need to remove the <filter> element from the xml definition and call the Apply method of the View object:
Private Sub btnClearFilter_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClearFilter.Click
Dim CurrentExplorer As Outlook.Explorer = Nothing
Dim CurrentView As Outlook.View = Nothing
Dim CurrentXML As XmlDocument = New XmlDocument
Dim CurrentFilterNodes As XmlNodeList
Dim CurrentFilterNode As XmlNode
CurrentExplorer = TryCast(ExplorerObj, Outlook.Explorer)
If (CurrentExplorer IsNot Nothing) Then
CurrentView = CurrentExplorer.CurrentView
If CurrentView IsNot Nothing Then
Try
CurrentXML.LoadXml(CurrentView.XML)
CurrentFilterNodes = CurrentXML. _
GetElementsByTagName("filter")
If CurrentFilterNodes.Count > 0 Then
For y As Integer = CurrentFilterNodes.Count - 1 _
To 0 Step -1
CurrentFilterNode = CurrentFilterNodes(y)
If CurrentFilterNode.HasChildNodes Then
For i As Integer = _
CurrentFilterNode.ChildNodes.Count - 1 _
To 0 Step -1
CurrentFilterNode.RemoveChild( _
CurrentFilterNode.ChildNodes(i))
Next
End If
Next
End If
CurrentView.XML = CurrentXML.InnerXml()
CurrentView.Apply()
Finally
Marshal.ReleaseComObject(CurrentView)
End Try
End If
End If
End Sub
That’s it! But that’s not the only element contained in the XML property. Good luck with the rest of them!
You may also be interested in:
Outlook customization: Reading pane, Outlook bar and To-Do bar
Creating Outlook add-in step-by-step
Available downloads:
C# sample Outlook add-in for VS 2005
VB.NET sample Outlook add-in for VS 2005





8 Comments
On calling currentView.Apply(), my view is not getting applied, I tried saving the view, and got the following error on Imap mailbox, this works fine for exchange mailbox
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Interop.Outlook.View.Save()
Any idea?
After calling the Apply method you can try to programmatically switch to another folder (say Outbox) and then switch back to your IMap mailbox. I think this will help apply your changes.
I’m trying to get a list of the elements contained in this .xml property and was pointed to http://msdn.microsoft.com/en-us/library/bb207155.aspx by Eugene.. unfortunately this article doesn’t list all of the elements or their accepted values.
I was wondering if anyone had a more comprehensive list? Googling doesn’t seem to reveal anything useful
As far as I remember there is no documentation that contains the description of all the elements. I can suggest adding a field (or making other changes) manually and then analyzing the changes in the XML property.
When I try to lunch Outlook 2007, I get an error message “can not view Outlook, invalid XML”. Any ideas?
I have just retested the sample with Outlook 2007 SP2. I applied and removed several filters but did not see any error messages. Did you change the source code of the sample?
I was wodering if anyone know how to get the count of filetring contacts items
You can try to use the Items.Restrict method. This method returns a filtered collection and in this way you will get the Count property.