Remove filter from current view - outlook 2003

C

Chris Quayle

Hi,

I need to create a macro that will clear filters from the current view. I
believe that this should be achieved by manipulating the xml. I started out
with the following:

Sub clearFiltersCurrentView()

Dim strXML As String
Dim intFilterStart As Integer
Dim intFilterEnd As Integer

strXML = Application.ActiveExplorer.CurrentView.XML

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Do While (intFilterStart <> 0 Or intFilterEnd <> 0)

If (intFilterStart = 0 Or intFilterEnd = 0) Then MsgBox "error"

strXML = Left(strXML, intFilterStart - 1) & Right(strXML, Len(strXML) -
(intFilterEnd - 1) - Len("</filter>"))

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Loop

Application.ActiveExplorer.CurrentView.XML = strXML

End Sub

Unfortunately, even though my strXML appears valid XML it doesn't seem to
remove the filter info from the XML of the current view. I have tried various
other methods to no avail. A simple line such as:

Application.ActiveExplorer.CurrentView.XML =
replace(Application.ActiveExplorer.CurrentView.XML,"UK","France")

works perfectly in changing the filter keyword from UK to France. However I
can't work out how to remove the filter altogether.
I've also noticed that if I apply a filter to a view, remove it and then
examine the output of Application.ActiveExplorer.CurrentView.XML the filter
information is still there even though it is not active. It also tends to
move to a different line in the XML (although my understanding is that this
makes no difference). It's as if there's another variable in the XML that
tells the view whether to apply the filter but I can't find it.

Please help!
 
K

Ken Slovak - [MVP - Outlook]

Removing the <filter> </filter> text will remove the filter. However, you
are not saving the view and applying it after you change it. You need to do
that to persist your changes and to make them permanent.

After you set the new View.XML you should get the View object and use its
Save(), then Apply() methods.

In Outlook 2007 you could just get the View object and use the new Filter
property set to null string before using Save() and Apply().
 
C

Chris Quayle

Ken,

Thanks for confirming that removing the filter text is the correct way. I
had removed the save/apply lines because I've found that they are not
necessary when changing other aspects of the xml. I have now added them and
my code still does not work. Code is as follows:
Sub clearFiltersCurrentView()

Dim strXML As String
Dim intFilterStart As Integer
Dim intFilterEnd As Integer
Dim vCurrentView As View

Set vCurrentView = Application.ActiveExplorer.CurrentView

strXML = vCurrentView.XML

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Do While (intFilterStart <> 0 Or intFilterEnd <> 0)

If (intFilterStart = 0 Or intFilterEnd = 0) Then MsgBox "error"

strXML = Left(strXML, intFilterStart - 1) & Right(strXML, Len(strXML) -
(intFilterEnd - 1) - Len("</filter>"))

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Loop

If InStr(1, strXML, "<sort>desc</sort>", vbTextCompare) Then
strXML = Replace(strXML, "<sort>desc</sort>", "<sort>asc</sort>")
Else
strXML = Replace(strXML, "<sort>asc</sort>", "<sort>desc</sort>")
End If

Debug.Print strXML
vCurrentView.XML = strXML
vCurrentView.Save
vCurrentView.Apply
Debug.Print vCurrentView.XML
End Sub

I have added the step to toggle the sorting order to be sure that strXML is
being applied and the column sorting in my outlook view is indeed inverting
every time I run it. The Debug.Print strXML output verifies that there is no
<filter> line in the new XML, however it persists in vCurrentView.XML even
after the changes are being applied. It's as if VBA will only change the XML
for XML tags that you offer a replacement for but won't delete them.

So I'm still stuck!

Chris
 
K

Ken Slovak - [MVP - Outlook]

This isn't Outlook 2007 is it?

I'm not sure this idea will work:

Get the XML, Name, ViewType and SaveOption property values. Also capture the
LockUserChanges value. Change the current view to another of the views
available in the Views collection Then go up one level to the Views
collection and delete that view. Add your view with all the properties you
saved, using the Add() function. Then drop down to the View object you got
from Add() and set the view and apply and save it.

See if that does something for you.
 
C

Chris Quayle

Thanks for your help. In the end I created a new view called 'Unfiltered
view' and applied strXML (with fliter text removed) to that view and loaded
it each time I want to clear fliters. Then I use a separate view 'Filtered
view' to be loaded every time I want to apply a filter and use macros to make
sure a filter never gets applied to Unfiltered.
 
J

Joe Joe

Here is the code I used in Outlook 2010, taken mostly from help :

Sub NOFilter()

Dim objView As View

' Obtain a View object reference to the current view.
Set objView = Application.ActiveExplorer.CurrentView

objView.Filter = ""

' Save and apply the view.
objView.Save
'objView.Apply ' Apply through an error - but the filter was cleared when I removed it.
End Sub
============================

In Outlook 2003, I never was able to complete remove the filter. I just set it to display all items received after Jan 1, 1970

I took a different approach though to modifying the XML.

Outlook 2003 code:
---------------------------

Sub FilterFromBoss()

Dim sFilter As String

sFilter = "urn:schemas:httpmail:fromname LIKE '%Boss, Sir%'"

Call ApplyFilter(sFilter)

End Sub




Sub ApplyFilter(sFilter As String)

Dim objOutlook As Outlook.Application
Dim objActExpl As Outlook.Explorer
Dim olfolder As Outlook.MAPIFolder

Dim objView As Outlook.View
Dim colViews As Outlook.Views
Dim objXMLDOM As New MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMNode
Dim strElement As String
Dim loadRst As Boolean

Set objOutlook = CreateObject("Outlook.Application")
Set objActExpl = objOutlook.ActiveExplorer
Set colViews = objActExpl.CurrentFolder.Views
Set olfolder = objActExpl.CurrentFolder
Set objView = olfolder.CurrentView

objView.LockUserChanges = False 'Allow changes to the view


'Load the XML for the new view into XML DOM object
loadRst = objXMLDOM.loadXML(objView.XML)

strElement = "view/filter"

Set objNode = objXMLDOM.selectSingleNode(strElement)

objNode.nodeTypedValue = sFilter

objView.XML = objXMLDOM.XML 'Assign the XML for View to objXML.XML

objView.Save 'Save the View
objView.Apply 'Apply the View in the folder

End Sub


' ** Finally to clear the filter
Sub FilterClear()

Dim sFilter As String

sFilter = "urn:schemas:httpmail:datereceived >= '1/1/1970'"

Call ApplyRestriction(sFilter)


End Sub



However, Outlook still reported "Filter Applied", which technically was true, no items were BEFORE 1970.


Hi,

I need to create a macro that will clear filters from the current view. I
believe that this should be achieved by manipulating the xml. I started out
with the following:

Sub clearFiltersCurrentView()

Dim strXML As String
Dim intFilterStart As Integer
Dim intFilterEnd As Integer

strXML = Application.ActiveExplorer.CurrentView.XML

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Do While (intFilterStart <> 0 Or intFilterEnd <> 0)

If (intFilterStart = 0 Or intFilterEnd = 0) Then MsgBox "error"

strXML = Left(strXML, intFilterStart - 1) & Right(strXML, Len(strXML) -
(intFilterEnd - 1) - Len("</filter>"))

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Loop

Application.ActiveExplorer.CurrentView.XML = strXML

End Sub

Unfortunately, even though my strXML appears valid XML it doesn't seem to
remove the filter info from the XML of the current view. I have tried various
other methods to no avail. A simple line such as:

Application.ActiveExplorer.CurrentView.XML =
replace(Application.ActiveExplorer.CurrentView.XML,"UK","France")

works perfectly in changing the filter keyword from UK to France. However I
can't work out how to remove the filter altogether.
I've also noticed that if I apply a filter to a view, remove it and then
examine the output of Application.ActiveExplorer.CurrentView.XML the filter
information is still there even though it is not active. It also tends to
move to a different line in the XML (although my understanding is that this
makes no difference). It's as if there's another variable in the XML that
tells the view whether to apply the filter but I can't find it.

Please help!
On Friday, March 27, 2009 10:52 AM Ken Slovak - [MVP - Outlook] wrote:
Removing the <filter> </filter> text will remove the filter. However, you
are not saving the view and applying it after you change it. You need to do
that to persist your changes and to make them permanent.

After you set the new View.XML you should get the View object and use its
Save(), then Apply() methods.

In Outlook 2007 you could just get the View object and use the new Filter
property set to null string before using Save() and Apply().




news:[email protected]...
On Friday, March 27, 2009 11:59 AM ChrisQuayl wrote:
Ken,

Thanks for confirming that removing the filter text is the correct way. I
had removed the save/apply lines because I've found that they are not
necessary when changing other aspects of the xml. I have now added them and
my code still does not work. Code is as follows:
Sub clearFiltersCurrentView()

Dim strXML As String
Dim intFilterStart As Integer
Dim intFilterEnd As Integer
Dim vCurrentView As View

Set vCurrentView = Application.ActiveExplorer.CurrentView

strXML = vCurrentView.XML

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Do While (intFilterStart <> 0 Or intFilterEnd <> 0)

If (intFilterStart = 0 Or intFilterEnd = 0) Then MsgBox "error"

strXML = Left(strXML, intFilterStart - 1) & Right(strXML, Len(strXML) -
(intFilterEnd - 1) - Len("</filter>"))

intFilterStart = InStr(1, strXML, "<filter>", vbTextCompare)
intFilterEnd = InStr(1, strXML, "</filter>", vbTextCompare)

Loop

If InStr(1, strXML, "<sort>desc</sort>", vbTextCompare) Then
strXML = Replace(strXML, "<sort>desc</sort>", "<sort>asc</sort>")
Else
strXML = Replace(strXML, "<sort>asc</sort>", "<sort>desc</sort>")
End If

Debug.Print strXML
vCurrentView.XML = strXML
vCurrentView.Save
vCurrentView.Apply
Debug.Print vCurrentView.XML
End Sub

I have added the step to toggle the sorting order to be sure that strXML is
being applied and the column sorting in my outlook view is indeed inverting
every time I run it. The Debug.Print strXML output verifies that there is no
<filter> line in the new XML, however it persists in vCurrentView.XML even
after the changes are being applied. It's as if VBA will only change the XML
for XML tags that you offer a replacement for but won't delete them.

So I'm still stuck!

Chris

"Ken Slovak - [MVP - Outlook]" wrote:
On Friday, March 27, 2009 4:39 PM Ken Slovak - [MVP - Outlook] wrote:
This isn't Outlook 2007 is it?

I'm not sure this idea will work:

Get the XML, Name, ViewType and SaveOption property values. Also capture the
LockUserChanges value. Change the current view to another of the views
available in the Views collection Then go up one level to the Views
collection and delete that view. Add your view with all the properties you
saved, using the Add() function. Then drop down to the View object you got
from Add() and set the view and apply and save it.

See if that does something for you.




news:[email protected]...
On Monday, March 30, 2009 10:16 AM ChrisQuayl wrote:
Thanks for your help. In the end I created a new view called 'Unfiltered
view' and applied strXML (with fliter text removed) to that view and loaded
it each time I want to clear fliters. Then I use a separate view 'Filtered
view' to be loaded every time I want to apply a filter and use macros to make
sure a filter never gets applied to Unfiltered.

"Ken Slovak - [MVP - Outlook]" wrote:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top