Results 1 to 13 of 13
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371

    Search Emails (Unread Mails Only)

    Hi Guy's, wilst i have the code below that filters an emai search, is there a simple method to only show unread emails for the given search string ?

    I have had a look on DevHut and a couple of Micrisoft sites, struggling to find the best method of applying filter (unread only)

    Here is my search, unsure if i can just add a small addiition to apply filter here:

    Code:
    SearchString = "Some Subject Or Some Email Address"
    
        Set app = GetObject(, "Outlook.Application")
        On Error GoTo 0
        If app Is Nothing Then
            Set app = CreateObject("Outlook.Application")
            app.Explorers.Add app.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
            app.Explorers(1).Activate
        End If
        
        app.ActiveExplorer.Search SearchString, olSearchScopeAllFolders
    
    
        Set app = Nothing
        DoCmd.RunCommand acCmdAppMinimize
    Thanks guy's

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,569
    Look at these links.
    https://www.google.com/search?q=filt...hrome&ie=UTF-8

    If there is not a way to filter for unread, there is an Unread property.
    https://learn.microsoft.com/en-us/of...ailitem.unread

    Edit:
    This seems the neatest?
    https://stackoverflow.com/questions/...ls-not-working
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thanks WGM, I did look at DevHut, i am dragged away from this now for a few hours but will look at your suggested links a little later

    Kindest

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,569
    Quote Originally Posted by DMT Dave View Post
    Thanks WGM, I did look at DevHut, i am dragged away from this now for a few hours but will look at your suggested links a little later

    Kindest
    Look at the last link, seems simplest and the neatest.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Just had a brief look, yes doesn't look much at all to that, looks fairly simple, even in my books

    I will read through better tonight and test, but again, always appreciated responses

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Just followng WGM link

    I can't find a missing reference in the library for MAPIFolder, do you know what refence require selecting ?

    Code:
        Dim olNs As Outlook.NameSpace    Dim Inbox As Outlook.MAPIFolder
        Dim Items As Outlook.Items
        Dim i As Long
        Dim Filter As String
    
    
        Set olNs = Application.GetNamespace("MAPI")
        Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    
    
        Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:hasattachment" & _
                           Chr(34) & "=1 AND " & _
                           Chr(34) & "urn:schemas:httpmail:read" & _
                           Chr(34) & "=0"
    
    
        Set Items = Inbox.Items.Restrict(Filter)
    
    
        For i = Items.Count To 1 Step -1
            Debug.Print Items(i) 'Immediate Window
        Next

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Use Folder instead, MAPIFolder was deprecated by MS a while ago.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,569
    Quote Originally Posted by DMT Dave View Post
    Just followng WGM link

    I can't find a missing reference in the library for MAPIFolder, do you know what refence require selecting ?

    Code:
        Dim olNs As Outlook.NameSpace    Dim Inbox As Outlook.MAPIFolder
        Dim Items As Outlook.Items
        Dim i As Long
        Dim Filter As String
    
    
        Set olNs = Application.GetNamespace("MAPI")
        Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    
    
        Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:hasattachment" & _
                           Chr(34) & "=1 AND " & _
                           Chr(34) & "urn:schemas:httpmail:read" & _
                           Chr(34) & "=0"
    
    
        Set Items = Inbox.Items.Restrict(Filter)
    
    
        For i = Items.Count To 1 Step -1
            Debug.Print Items(i) 'Immediate Window
        Next
    Remember, that code is for Outlook and it works completely fine for me, so much so, I have saved it in case I ever need to do something like that.
    I am however using 2007 Outlook.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Sorry, forgot to add a link:
    https://stackoverflow.com/questions/...in-vba-project
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi WGM and Vlad, thank you both

    @WGM, so the code needs to go in outlook code ? not access ? thats if i have read your reply correct

    thanks Vlad, i will try using folder instead and look at the refrerences you ahve sent on the link..

    Kindest both of you

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,569
    Quote Originally Posted by DMT Dave View Post
    Hi WGM and Vlad, thank you both

    @WGM, so the code needs to go in outlook code ? not access ? thats if i have read your reply correct

    thanks Vlad, i will try using folder instead and look at the refrerences you ahve sent on the link..

    Kindest both of you
    No. That code is in Outlook.
    No reason why you cannot run it from Access, but you would need to set an object variable to an instance of Outlook.
    The fact that nown is in that code, or mentioned in the link, leads me to believe that is Outlook VBA.

    However nothing to say that cannot be run from Access ?, it is just VBA after all.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Yeh, im still getting method or data member not found, highlighted red is not liisted as an option when re enter .(dot)

    Added vlad's suggestion in ref library
    change MAPI Folder to Folder in my Dim

    But now thinking, this is not for access, just outlook code (per WGM respinse), if im understanding correct

    Code:
    Dim olNs As Outlook.NameSpace    Dim Inbox As Outlook.Folder
        Dim Items As Outlook.Items
        Dim i As Long
        Dim Filter As String, strAddress As String
    
    
    
    
        Set olNs = Application.GetNamespace("MAPI")
        Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
        
        Debug.Print Inbox
        
        
        strAddress = "Some Email Subject"
    
    
        Filter = "@SQL=" & Chr(34) & strAddress & ":read" & _
                           Chr(34) & "=0"
    
    
    
    
        Set Items = Inbox.Items.Restrict(Filter)
    
    
    
    
        For i = Items.Count To 1 Step -1
            Debug.Print Items(i) 'Immediate Window
        Next

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,569
    That is not going to work anyway as you have not included the schema?
    I do not know if that subject has to be included as criteria with the schema as well? I would expect so as it was needed for the attachments property in that link code.

    If you are running this in Access, then Access is the application and that will not have a Namespace?

    You have to understand the code you are trying to use. Just slapping bits together is never going to work.

    https://learn.microsoft.com/en-us/of...n.getnamespace

    Here is what I think is a badly named variable for calling from another app. I would have used anything BUT application, even if it is all in lowercase.
    https://learn.microsoft.com/en-us/of...nce-of-outlook
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. processing e-mails from outlook into access
    By Mlki in forum Programming
    Replies: 1
    Last Post: 07-11-2016, 09:40 AM
  2. Replies: 4
    Last Post: 08-10-2015, 03:55 PM
  3. Sending E-mails, new problem just started
    By Jacob.T in forum Import/Export Data
    Replies: 0
    Last Post: 08-01-2013, 03:04 PM
  4. Sending customized e mails on update
    By Alpana in forum Access
    Replies: 5
    Last Post: 01-25-2012, 12:46 AM
  5. Send e-mails using Access
    By Dotty in forum Queries
    Replies: 4
    Last Post: 09-30-2011, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums