• Set view filters with VBA (Outlook 2002)

    Home » Forums » AskWoody support » Productivity software by function » MS Outlook and email programs » Set view filters with VBA (Outlook 2002)

    Author
    Topic
    #412602

    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?

    Viewing 5 reply threads
    Author
    Replies
    • #903708

      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. smile

    • #903709

      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. smile

    • #903712

      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.

    • #903713

      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.

    • #903976

      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):

      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
      

      Called with something like:

      Sub RemoveFilterFromView()
         Call SetTableProperties("Messages", "view/filter", "")
      End Sub
      

      Hope that helps.

      • #903990

        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?

        • #904000

          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:

          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
          

          called with

          Sub testit()
             Dim fdr As MAPIFolder
             Set fdr = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
             Call SetTableProperties(fdr, "Messages", "view/filter", "")
          End Sub
          
          • #904058

            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).

            • #904066

              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?

            • #904068

              It makes perfect sense.

              Sadly, it’s not quite what I’m trying to do. I’m as equally annoyed as Jamloo about the Show In Groups View 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?

            • #904069

              It makes perfect sense.

              Sadly, it’s not quite what I’m trying to do. I’m as equally annoyed as Jamloo about the Show In Groups View 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?

            • #904140

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

              Messages …
              … (many lines later)
              %today(“urn:schemas:httpmail:datereceived”)%

              How would I set:

              Messages …

              1

              to

              0

              Like this?

              SetTableProperties(fldr, “Messages”, “view/autogroup”, 0)

              I don’t know or understand the syntax.

            • #904189

              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. laugh

            • #904193

              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.

            • #904194

              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.

            • #904190

              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. laugh

            • #904215

              John,
              You are correct. I would have thought that

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

              should work – does it not?

            • #904235

              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

            • #904236

              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

            • #904239

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

              Call SetTableProperties(allSubFolders(lngC), “Messages”, “view/arrangement/autogroup”, “0”)

            • #904240

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

              Call SetTableProperties(allSubFolders(lngC), “Messages”, “view/arrangement/autogroup”, “0”)

            • #904568

              Glad you got it sorted. I think I may have to reset my view as my inbox is now a mess of various coloured fonts on a somewhat garish orange background… grin

            • #904576

              Rory, thanks for your help and patience on this.

              Did you try Jefferson’s code here, and any ideas on what the problem was in that thread?

            • #904872

              I’ve just had a quick look (and I hasten to add I’ve been out most of the night at a leaving do! cheers headthrob) and the only thing I can see initially that might be an issue is that I couldn’t spot an explicit command to apply the modified view. I will try and do some testing tomorrow to see if that is relevant.

              Later:
              All tests on OL2002 seem to work perfectly as is, so I will see if I can find any information as to why it would not work in 2003. I may even finally get around to installing the beta of Office2003 that I have lying around onto a test machine.

            • #904873

              I’ve just had a quick look (and I hasten to add I’ve been out most of the night at a leaving do! cheers headthrob) and the only thing I can see initially that might be an issue is that I couldn’t spot an explicit command to apply the modified view. I will try and do some testing tomorrow to see if that is relevant.

              Later:
              All tests on OL2002 seem to work perfectly as is, so I will see if I can find any information as to why it would not work in 2003. I may even finally get around to installing the beta of Office2003 that I have lying around onto a test machine.

            • #904577

              Rory, thanks for your help and patience on this.

              Did you try Jefferson’s code here, and any ideas on what the problem was in that thread?

            • #904569

              Glad you got it sorted. I think I may have to reset my view as my inbox is now a mess of various coloured fonts on a somewhat garish orange background… grin

            • #904216

              John,
              You are correct. I would have thought that

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

              should work – does it not?

            • #904141

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

              Messages …
              … (many lines later)
              %today(“urn:schemas:httpmail:datereceived”)%

              How would I set:

              Messages …

              1

              to

              0

              Like this?

              SetTableProperties(fldr, “Messages”, “view/autogroup”, 0)

              I don’t know or understand the syntax.

            • #904067

              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?

          • #904059

            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).

        • #904001

          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:

          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
          

          called with

          Sub testit()
             Dim fdr As MAPIFolder
             Set fdr = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
             Call SetTableProperties(fdr, "Messages", "view/filter", "")
          End Sub
          
      • #903991

        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?

    • #903977

      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):

      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
      

      Called with something like:

      Sub RemoveFilterFromView()
         Call SetTableProperties("Messages", "view/filter", "")
      End Sub
      

      Hope that helps.

    Viewing 5 reply threads
    Reply To: Set view filters with VBA (Outlook 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: