Home » excel » excel – Send/Receive in Outlook via code

excel – Send/Receive in Outlook via code

Posted by: admin April 23, 2020 Leave a comment

Questions:

If I create an Outlook 2010 object In Excel VBA using

Sub CreateOL()
    On Error Resume Next
        Set myOlApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set myOlApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
End Sub

Is it possible to force myOLAPP to send/receive. Please can somebody advise how it’s done?

I’ve tried the following but it’s not working for me.

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

    syc.Start

 Next

Also, how do I make myOlApp visible? myOlApp.Visible = True and myOlApp.Application.Visible = True doesn’t work

Thank you

How to&Answers:

Which Outlook version are you using? I tested this with Outlook 2010 and it works.

NOTE: I have not done any error handling. I am sure you can take care of that.

Public Sub Sample()
    Dim oLook As Object
    Dim nsp As Object, objSyncs As Object, objSync As Object
    Dim i As Long

    Set oLook = GetObject(, "Outlook.Application")

    Set nsp = oLook.GetNamespace("MAPI")

    Set objSyncs = nsp.SyncObjects

    For i = 1 To objSyncs.Count
        Set objSync = objSyncs.Item(i)
        objSync.Start
    Next
End Sub

MS Outlook has 2 types of windows

  1. Explorer for Folders and
  2. Inspector for individual items.

If you want to show a particular folder, you can start the Explorer for it then either use .Activate or .Display. An alternative would be to use MAPIFolder.Display method as well.