Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send/Receive in Outlook via code

Tags:

excel

vba

outlook

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


1 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.

like image 146
Siddharth Rout Avatar answered Oct 27 '25 13:10

Siddharth Rout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!