refresh Outlook Inbox via vba (1 Viewer)

supmktg

Registered User.
Local time
Today, 09:02
Joined
Mar 25, 2002
Messages
360
I am using the following code to refresh my Outlook Inbox before searching it for responses to certain emails:

Code:
Public Sub RefreshInbox()
 
 Dim nsp As Outlook.NameSpace
 Dim sycs As Outlook.SyncObjects
 Dim syc As Outlook.SyncObject
 Dim i As Integer
 
   On Error GoTo RefreshInbox_Error

 Set nsp = Outlook.GetNamespace("MAPI")
 Set sycs = nsp.SyncObjects
 
 For i = 1 To sycs.Count
  
 Set syc = sycs.Item(i)

 syc.start
  
 Next
 
 syc.Stop

 Set nsp = Nothing
 Set sycs = Nothing
 Set syc = Nothing

   On Error GoTo 0
   Exit Sub

RefreshInbox_Error:

    If err.Number = 462 Then
    err.Clear
    Else
    MsgBox "Error " & err.Number & " (" & err.Description & ") in procedure RefreshInbox of Module nsReadInbox"
    End If
End Sub

I'm having 2 issues with the code which both seem to me to be caused by a delay in the communication between Outlook and Access.

The first is that the code often throws an error 462 (remote server unavailable), which I understand from searching for a solution is a common problem when running other Office applications via vba. I've resolved that issue as best I could by trapping and clearing the error.

The second issue bewilders me. There seems to be about a 15 minute delay between when an email is received and when shows up in the Outlook inbox. I have this Outlook email account set up so that emails are forwarded to another non-Outlook account. Even after having received an email in the forwarded account inbox, the above code does not seem to receive it in the Outlook inbox until about 15 minutes later. Does this make sense? Is there something I can do to eliminate the delay?

Any help would be appreciated!

Sup
 

darbid

Registered User.
Local time
Today, 15:02
Joined
Jun 26, 2008
Messages
1,428
Whenever I get the MAPI namespace I always call the logon method.

Code:
oNS.Logon "", "", False, True
This will of course depend on whether you are using GetObject or CreateObject.

I have never used the SyncObjects object but I just did a little reading and am not sure it is necessary for you to use.

Is there a reason why you dont just use "sendandreceive" method? https://msdn.microsoft.com/en-us/library/office/ff861834.aspx
 

supmktg

Registered User.
Local time
Today, 09:02
Joined
Mar 25, 2002
Messages
360
Thank you Darbid! I seldom use Outlook automation, but needed to automate an approval process based on returned emails. I am not that familiar with all of the Outlook methods.

As far as sync vs sendandreceive is concerned, I have at times manually hit the Send/Receive button in outlook only to find that exchange wasn't ready to move it to my inbox. The send/receive or sync issue is a minor frustration. I found it hard to believe that a forwarded email would hit my non-outlook email inbox before that email made it to my outlook inbox. I was looking for instant gratification, and I think I just need to realize that exchange on Sharepoint takes more time than I would like.

I will try the logon method to see if that prevents the 462 error, which will make me very happy.

Thanks,
Sup
 

darbid

Registered User.
Local time
Today, 15:02
Joined
Jun 26, 2008
Messages
1,428
I know zero about email servers. Please keep that in mind.

In my opinion Outlook is just a client to display your emails. As such it can hook up with many different email sources (Hotmail / iCloud / MSExcahnge etc etc). I have noticed especially with the free email providers that they do not always supply updates to email. They have it time limited. Thus I think your problem does not lie with Outlook but with your servers.

Everything you code is only controlling Outlook (the client) so I am not sure hitting sync or sendandreceive or coding it is going to help the server push new mail to Outlook in many cases.
 

Users who are viewing this thread

Top Bottom