Apr 14 2023 05:01 AM
Hello everyone,
I received several requests for my sample Access application for creating appointments in Outlook, so I am sharing it here.
The application
The application automatically opens the Action form. When you complete this form and click Add to Outlook, some VBA code in the application creates an appointment in Outlook based on the action data and checks the Added to Outlook setting.
The VBA code
Select Database Tools > Visual Basic to open the underlying VBA code. If needed, expand the Project folder:
And double click the Form_Actions object to display the VBA code:
Private Sub cmdAddToOutlook_Click()
' Only add an appointment to Outlook if it has not been done before.
If Me.AddedToOutlook = False Then
' Declare objects for the Outlook application and an appointment that you can address programmatically
Dim xOL As Outlook.Application, itmAppoint As Outlook.AppointmentItem
' Create the actual Outlook object and an object for the appointment to be added
Set xOL = New Outlook.Application
Set itmAppoint = xOL.CreateItem(olAppointmentItem)
' Fill the appointment details and send it to Outlook
With itmAppoint
.Subject = Me.ActionDescription
.Start = Me.ActionDate + Me.ActionTime
.End = Me.ActionDate + Me.ActionTime + Me.ActionLength
.Location = Me.ActionLocation
.Save ' <- this actually sends the appointment to Outlook
End With
' Remove the objects to keep memory clean
Set itmAppoint = Nothing
Set xOL = Nothing
' Set the checkbox
Me.AddedToOutlook = True
' Requery the form to show all data changes
Me.Requery
End If
End Sub
Note
I set a reference to the Outlook Object Library from VBA, but if you receive an error that the reference is missing, follow these steps from VBA:
Apr 14 2023 11:31 AM
May 23 2023 08:38 AM
Forgive the rookie question. Once I save this, how do I add to my existing database?
Thanks!
Jun 01 2023 08:38 AM
Jun 14 2023 11:44 AM
Nov 02 2023 01:10 AM
@Tieme Woldman Thanks so much for the VBA code to add to Outlook, it works great. I dont suppose you have the code to remove an appointment from Outlook using access do you?
Nov 02 2023 01:54 AM
@craigjacks - a good ideea, I am intereted too!
Nov 04 2023 05:56 AM
Feb 05 2024 09:33 AM
Thank @Tieme Woldman , great help is this code !
Feb 28 2024 02:08 AM
I've also added functionality to delete an appointment from Outlook. In the sample application, you'll now find a delete button in addition to the add button.
This is the underlying code, which deletes an appointment if the subject and start date/time match:
Private Sub cmdDeleteFromOutlook_Click()
Dim xOL As Outlook.Application, itmAppoint As Outlook.AppointmentItem, colAppoint As Outlook.Items
Dim nmsOL As Outlook.NameSpace, fldOL As Outlook.MAPIFolder
Dim i As Integer
' Create the actual Outlook object and an object for the appointment to be deleted
Set xOL = New Outlook.Application
Set itmAppoint = xOL.CreateItem(olAppointmentItem)
Set nmsOL = xOL.GetNamespace("MAPI")
Set fldOL = nmsOL.GetDefaultFolder(olFolderCalendar)
' Loop over all appointments
Set colAppoint = fldOL.Items
For i = colAppoint.Count To 1 Step -1
Set itmAppoint = colAppoint.Item(i)
' Delete appointment if description and start data/time match
If Me.ActionDescription = itmAppoint.Subject And Format(Me.ActionDate, "yyyy-mm-dd") & " " & Format(Me.ActionTime, "hh:mm") = Format(itmAppoint.Start, "yyyy-mm-dd hh:mm") Then
itmAppoint.Delete
' Reset the checkbox
Me.AddedToOutlook = False
' Requery the form to show all data changes
Me.Requery
End If
Next
' Remove the objects to keep memory clean
Set itmAppoint = Nothing
Set colAppoint = Nothing
Set nmsOL = Nothing
Set fldOL = Nothing
Set xOL = Nothing
End Sub
This code also resets the 'Added to Outlook' flag in the Access database.
I hope this helps.
Tieme
Feb 28 2024 02:10 AM
Feb 28 2024 02:10 AM