none
Outlook refresh view from VBA RRS feed

  • Question

  • I find a lot of questions similar to this, but nowhere an answer except for "it can't be done" (which is actually not 100% correct, as you'll see below).

    I have a macro that I use mostly when in the "unread items" search view. It scans though a number of subfolders of my inbox, searches for specific messages there, marks them read, and finally sends an F5 keystroke to refresh the unread items view.

    The problem is in the final part: that only works if the message list has the focus when the macro is started.

    So my question is

    • Is there a way to force-refresh the message list, other than sending it an F5 keystroke?
    • If there isn't, is there a way to programmatically set the focus to the message list?
    • Or to send a keystroke to a specific window pane, instead of just "to the window"?

    BTW, in case anyone would be searching for a way to refresh, and my existing solution would be sufficient (i.e. works only if message list has the focus): just sending an F5 right after modifying message properties doesn't fully do the trick, it misses a few messages sometimes. I found that it works better if you include a short delay (1/4 second) before sending the F5.

    The delay can be obtained through the Sleep() API function, so include this at the module level (comment one line out, depending on whether you're running the 32 or 64 bit version of Outlook - that will be 32 for most, also on 64 bit systems):

    ' Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongLong) ' 64 bit version
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ' 32 bit version

    To implement the delay and start the refresh, in the sub where you want to do it:

        Sleep 250
        SendKeys "{F5}"
    

    Wednesday, May 18, 2016 6:43 AM

Answers

  • Hi lucvdv,

    Thanks for your details information, and I could reproduce your issue at my side. But, for your previous questions, I am afraid they are impossible. There is no direct api to achieve this requirements.

    For your fundamental purpose, I think there is no need to use vba, I suggest you try outlook rules to mark incoming emails as read. You could refer Manage email messages by using rules for more information.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by lucvdv Friday, May 20, 2016 8:16 AM
    Friday, May 20, 2016 2:44 AM

All replies

  • Hi lucvdv,

    >> finally sends an F5 keystroke to refresh the unread items view.

    Why do you want to refresh unread items? If you do not refresh, do you have any unexpected behavior? I made a test with below code, it makes mailItem as read in Unread Folder, it works correctly without sending F5.

    Sub getSearchFolder()
        Dim objStores As Outlook.Stores
        Dim objStore As Outlook.Store
        Dim objSearchFolders As Outlook.Folders
        Dim objFolder As Folder
        Dim objMailItem As MailItem
        Dim objMailItembak As MailItem
        Set objStore = Application.Session.Stores(1)
        Set objSearchFolders = objStore.GetSearchFolders
        Set objFolder = objSearchFolders.Item("Unread Mail")
        'Debug.Print objFolder.items.Count
        For Each objMailItem In objFolder.items
            Debug.Print objMailItem.Subject
            If objMailItem.Subject = "case meeting " Then
                objMailItem.UnRead = False
            End If
        Next objMailItem
    End Sub

    It would be helpful if you could share us more details steps to reproduce your issue.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, May 19, 2016 2:31 AM
  • I refresh the view to remove read items from the list.

    I receive about 150 automatically generated mails per day, that only need archiving and that I don't need to read all. Outlook is set to start in the "Unread Mail" search folder, the macro is there to remove all mails I don't have to read from that view so just the important ones remain.


    • Edited by lucvdv Thursday, May 19, 2016 5:54 AM
    Thursday, May 19, 2016 5:47 AM
  • Hi lucvdv,

    >> I refresh the view to remove read items from the list

    Do you mean if you do not refresh the view, the read items still exist in unread mail folder? What is your Outlook version? I made a test with my above code under Outlook 2013. After I setting UnRead property for the mailItem, it will disappear in the unread mail folder immediately.

    If I misunderstood you, please feel free to let me know.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, May 19, 2016 8:56 AM
  • That is what I mean, read messages remain in the unread mail list until I either refresh (F5) it or navigate to another folder and back to unread.

    This is in Outlook 2013 on Windows 10, but it was the same with older Outlook versions and on older OS versions.

    I include a screenshot below. These messages were marked read manually, but setting .Unread=False in script has the same effect.

    And BTW, the "unread" / "read" above the list work opposite to how I would expect. Changing from "all" to "unread" changes nothing, but changing from "unread" to "all" removes the read messages from the list.

    Thursday, May 19, 2016 2:19 PM
  • Hi lucvdv,

    Thanks for your details information, and I could reproduce your issue at my side. But, for your previous questions, I am afraid they are impossible. There is no direct api to achieve this requirements.

    For your fundamental purpose, I think there is no need to use vba, I suggest you try outlook rules to mark incoming emails as read. You could refer Manage email messages by using rules for more information.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by lucvdv Friday, May 20, 2016 8:16 AM
    Friday, May 20, 2016 2:44 AM
  • Thanks, that is what I expected, I just asked to be sure.

    As for marking them read by rules, that is out of the question for another reason.

    I am using rules to move messages (the same ones the macro marks read, as a matter of fact) from my inbox to other folders at arrival. As long as *no* rule contains "mark read" among its actions, the rules are processed by Exchange, so they are applied immediately upon arrival, even when Outlook is not running.

    When "mark read" is used in any rule, Exchange stops processing them and leaves that to Outlook instead, but it will keep pushing new messages to my phone. The result is that I get a notification alert for each mail, including the auto-generated ones. 

    When Exchange executes the rules, I only get notifications for mails that remain in my inbox after the rules are processed.


    • Edited by lucvdv Friday, May 20, 2016 6:15 AM
    Friday, May 20, 2016 6:14 AM
  • Hi lucvdv,

    It is so bad. If you use your original way, will outlook push new messages which include auto-generated to your phone? If rules are not suit for you, I am afraid there is no better way to work around your issue except manually select message list.

    For this issue, I would suggest you submit a feedback in the link below:

    https://outlook.uservoice.com/

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, May 20, 2016 7:21 AM
  • The way I do it now, those auto-generated mails are not pushed to my phone.

    It's not that much of a problem to select the right pane before I start the macro, but sometimes I forget it, so I thought it would not be a bad idea to try and find out of it can be done by script.

    It can't, so OK, now I know it can't and I'll just leave the matter to rest :)

    Friday, May 20, 2016 8:15 AM