Hello All,
This is my first time trying to control Outlook through Access VBA, and I have no idea what I'm doing. I have created a database for users to reserve building space, and I would like the command button on the form to enter their scheduled usage time on an Outlook calendar.
So far, I have created a new calendar (ATCG8S1) in Outlook, which shows up just below my personal calendar (LoveJ), under My Calendars. When everything is up & running with the database, my plan is to send a share invite for the calendar to my boss. He will be the person entering all of the usage times through the database instead of me. I don't know if this is the best way to make this happen, but being unfamiliar with Outlook calendars, it's all I've been able to come up with. Any other suggestions are welcome.
The Access form I'm starting with contains text boxes for the user to enter; txtUser, txtGrp, txtStartDate, txtEndDate. It also has txtStartTime and txtEndTime, but these boxes default to 0:00:01 AM & 11:59:59 PM respectively, as all usage will be an all day event. These last two textboxes will not be visible on the form.
I found the following code on one of the forums, and changed the names to fit my needs (I think)...
Private Sub cmdReserve_Click()
Dim oApp As Object
Dim oCalendar As Outlook.MAPIFolder
Dim oNameSpace As Outlook.NameSpace
Dim oAppt As Outlook.AppointmentItem
Dim oAcct As Outlook.Recipient
Dim ApptNotes As String
Dim strName As String
Dim calName As String
On Error Resume Next
calName = "ATCG8S1"
Set oApp = CreateObject("Outlook.Application.16")
Set oNameSpace = oApp.GetNamespace("Mapi")
Set oAcct = oNameSpace.CreateRecipient(calName)
Set oCalendar = oNameSpace.GetSharedDefaultFolder(oAcct, olFolderCalendar)
Set oAppt = oCalendar.Items.Add(olAppointmentItem)
'Save Appointment
With oAppt
.Subject = Me!txtUser & " " & Me!txtGrp
.Start = Format(Me.txtStartDate, "Short Date") &" " & Format(Me.txtStartTime, "Short Time")
.End = Format(Me.txtEndDate, "Short Date") & " " & Format(Me.txtEndTime, "Short Time")
.ReminderSet = False
.AllDayEvent = AllDay
.Save
.Close (olSave)
End With
MsgBox "Appointment Added!", vbInformation
'Release the Outlook object variable
Set oCalendar = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing
Err_Exit:
Set oCalendar = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing
Exit Sub
End Sub
Having no idea what any of the code means, I don't know to get this to work. The appointment does not get added to the calendar, but I am not getting any error messages. The only thing that does work is the popup message box at the end that says "Appointment Added"
As always, any help is appreciated.