Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Lounger
    Join Date
    Aug 2002
    Location
    London, Gtr London, England
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Set view filters with VBA (Outlook 2002)

    At work I have manually set a filter on a folder view (it shows emails with the word "loan" in the "subject") but this filter setting has cascaded from my Inbox across all sub folders in the Inbox, and private folders and public folders. Deleting the filter setting manually at the Inbox level does not repeat the cascade so I keep having to manually clear the filter at individual sub-folder level. I have several hundred sub folders so this is a drag. My company IT Support say they can not clear the problem.

    I have some vba code using the PickFolder function that will drill down through a folder / sub folder collection. However I can not identify from the Object Browser what object I need to manipulate to clear the View Filter for each folder (I am assuming each folder has an active view set with the filter on).

    Can anyone help with the vba syntax to manipulate view filters?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Set view filters with VBA (Outlook 2002)

    VBA cannot deal effectively with individual View settings. Your best bet is to make sure that the View itself is correctly defined and reapply it. Naturally, I haven't tested this idea. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    As Jefferson states, neither Views nor Filters are directly exposed in the VBA Object Model. The Outlook VBA Filter Mehod creates a Search, and the Items.Restrict Method does not change the View. Look here to see if there's anything that can be done with the XML View definitions, I'm out of my depth with it.
    -John ... I float in liquid gardens
    UTC -7ąDS

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Set view filters with VBA (Outlook 2002)

    Hi,
    Something like this, adapted from an MSDN article, will remove the filter from the selected view (in the example I have used Messages as the view; obviously if you use another view you will need to use that name instead):
    <pre>Sub SetTableProperties(ByVal strViewName As String, ByVal strProp As String, _
    ByVal strValue As String)

    Dim objViews As Views
    Dim objView As View
    'Set a reference to the MSXML 2.6 type library.
    Dim objXML As New MSXML2.DOMDocument
    Dim objXMLNode As MSXML2.IXMLDOMNode

    Set objViews = Outlook.Application.GetNamespace(Type:="MAPI") _
    .GetDefaultFolder(FolderType:=olFolderInbox).Views
    Set objView = objViews(strViewName)

    'Load the schema into the MSXML parser.
    objXML.loadXML bstrXML:=objView.XML

    'Select the node you want to modify and assign the new value.
    Set objXMLNode = objXML.selectSingleNode(querystring:=strProp)
    objXMLNode.nodeTypedValue = strValue

    'Copy the modified XML back to the new view.
    objView.XML = objXML.XML
    'Save and apply the new view.
    objView.Save
    objView.Apply

    End Sub
    </pre>

    Called with something like:
    <pre>Sub RemoveFilterFromView()
    Call SetTableProperties("Messages", "view/filter", "")
    End Sub
    </pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    Rory, I need help understanding this. If the coder wants to loop through a series of Folders, where/how is the target folder addressed? In the code you provided, are the views in the target Folder set to the Default Inbox view?
    -John ... I float in liquid gardens
    UTC -7ąDS

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Set view filters with VBA (Outlook 2002)

    John,
    You are correct - I did not address that as the OP indicated he already had code to iterate through the Folders and it's easily enough adapted to pass a folder variable - something like:
    <pre>Sub SetTableProperties(ByRef fdrFolder As MAPIFolder, ByVal strViewName As String, _
    ByVal strProp As String, ByVal strValue As String)

    Dim objViews As Views
    Dim objView As View
    'Set a reference to the MSXML 2.6 type library.
    Dim objXML As New MSXML2.DOMDocument
    Dim objXMLNode As MSXML2.IXMLDOMNode

    Set objViews = fdrFolder.Views
    Set objView = objViews(strViewName)

    'Load the schema into the MSXML parser.
    objXML.loadXML bstrXML:=objView.XML

    'Select the node you want to modify and assign the new value.
    Set objXMLNode = objXML.selectSingleNode(querystring:=strProp)
    objXMLNode.nodeTypedValue = strValue

    'Copy the modified XML back to the new view.
    objView.XML = objXML.XML
    'Save and apply the new view.
    objView.Save
    objView.Apply

    End Sub
    </pre>

    called with
    <pre>Sub testit()
    Dim fdr As MAPIFolder
    Set fdr = Application.GetNamespace("MAPI").GetDefaultFolder( olFolderInbox)
    Call SetTableProperties(fdr, "Messages", "view/filter", "")
    End Sub
    </pre>

    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    Sorry, Rory, I'm still not getting all of it. Now I follow how to pass the folder to the Function, using code such as the attached.

    What I don't understand is where the desired replacement View is set; I had the idea of selecting a folder which has the desired View, and getting it's View into the code, then passing it through your routine (tweaked as necessary).
    -John ... I float in liquid gardens
    UTC -7ąDS

  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Set view filters with VBA (Outlook 2002)

    Hi John,
    The view does not get replaced, it gets modified. I have assumed that the OP is using a standard view (such as "Messages" in my example) but has customised it with a filter. All this code does, is check the Messages View for the relevant folder and remove the filter aspect of it.
    Does that make sense?
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    It makes perfect sense.

    Sadly, it's not quite what I'm trying to do. I'm as equally annoyed as <!profile=Jamloo>Jamloo<!/profile> about the <!post=Show In Groups View,427775>Show In Groups View<!/post> in Outlook 2003, where that 'feature' cannot be turned off globally, or a View cascaded through a selection of Folders. So my question is can I create code that gets the active XML View from a specified Folder (in this case one where 'Show In Groups' is off) and replicate it to other folders of the same IPM class?
    -John ... I float in liquid gardens
    UTC -7ąDS

  10. #10
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    OK, I'm slowly getting smarter. The code you posted resets the filter string to "" in the following to the XML definitions, correct?

    <view type="table">
    <viewname>Messages</viewname> ...
    ... (many lines later)
    <filter>%today("urn:schemas:httpmail:datereceived" )%</filter>

    How would I set:

    <view type="table">
    <viewname>Messages</viewname> ...
    ...
    <autogroup>1</autogroup>

    to

    <autogroup>0</autogroup>

    Like this?

    SetTableProperties(fldr, "Messages", "view/autogroup", 0)

    I don't know or understand the syntax.
    -John ... I float in liquid gardens
    UTC -7ąDS

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Set view filters with VBA (Outlook 2002)

    I'm glad you guys are working on this one. After the trauma with gridline colors, I really don't want to go back there. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

  12. #12
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    I'm searching for your gridline colors post at this moment! So as to get a better understanding (starting from zero) of how the XML settings are tweaked.
    -John ... I float in liquid gardens
    UTC -7ąDS

  13. #13
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Set view filters with VBA (Outlook 2002)

    John,
    You are correct. I would have thought that <pre>Call SetTableProperties(fldr, "Messages", "view/autogroup", "0")</pre>

    should work - does it not?
    Regards,
    Rory

    Microsoft MVP - Excel

  14. #14
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    I have been concerned about hosing my Views, but upon trying it, I get a runtime error.

    Set objXMLNode = objXML.selectSingleNode(querystring:=strProp) ' objXMLNode is not set/remains Nothing
    objXMLNode.nodeTypedValue = strValue ' so this line errors out
    -John ... I float in liquid gardens
    UTC -7ąDS

  15. #15
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Set view filters with VBA (Outlook 2002)

    AHA! My mistake, it works! I missed part of the XML hierachy, it should have been:

    Call SetTableProperties(allSubFolders(lngC), "Messages", "view/<font color=red>arrangement/</font color=red>autogroup", "0")
    -John ... I float in liquid gardens
    UTC -7ąDS

Page 1 of 2 12 LastLast

Posting Permissions

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