Welcome to Eileen's Lounge!
Do the following in the Visual Basic Editor:
1) In Tools > References..,. tick the check box for Microsoft Outlook n.0 Object Library, then click OK.
n.0 depends on your version of Office:
12.0 for Office 2007
14.0 for Office 2010
15.0 for Office 2013
16/0 for Office 2016, Office 2019 and Microsoft 365
2) Select Insert > Class Module.
Name the class module clsOutlook.
Copy the following code into the class module:
Code: Select all
Option Explicit
Public WithEvents myOlApp As Outlook.Application
Public WithEvents mySync As Outlook.SyncObject
Private Sub Class_Initialize()
Set myOlApp = New Outlook.Application
Set mySync = myOlApp.Session.SyncObjects.Item(1)
mySync.Start
End Sub
Private Sub Class_Terminate()
Set mySync = Nothing
myOlApp.Quit
End Sub
Private Sub mySync_SyncEnd()
MsgBox "Synchronization is complete."
End Sub
3) Let's say you want to call the code from a command button on a form in an Access database.
Copy the following code into the form's module:
Code: Select all
Sub cmdSendReceive_Click()
Dim myOutlook As clsOutlook
Set myOutlook = New clsOutlook
End Sub