user857521
user857521

Reputation:

Send/Receive in Outlook via code

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

Upvotes: 4

Views: 6610

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149295

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.

Upvotes: 4

Related Questions