theConjurian
IS-IT--Management
I have a CRM type of application that manages contracts for an entertainment business. One of the functions creates a calendar object in Outlook. This has been running just fine for a long time. I am trying to implement synchronization via iCloud to my phone and tablet.
Unfortunately, this means that there is no longer any data calendar or contact data kept in the default Outlook folder. I need to change the coding to select this different folder that iCloud uses.
It appears that the folder is seen by the VBA code, but I am missing the key part to actually create the object here.
The relevant code currently in use follows. I have heavily annotated the code to indicate where I am digressing from the working code.
Any assistance would be greatly appreciated.
Private Sub updateOutlookAppointment()
Dim objOutlook As New Outlook.Application
Dim objCalendar As MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objCalendarItems As Outlook.Items
Dim objAppointment As Outlook.AppointmentItem
Dim folderName As String
Dim foundFolder As Folder
' The next two lines should be fine because they deal with higher level objects
Set objOutlook = CreateObject("Outlook.Application")
Set objAppointment = objOutlook.CreateItem(olAppointmentItem)
' OK, not sure about the next line in the iCloud side of things
Set objNameSpace = objOutlook.GetNamespace("MAPI")
' The problem starts here. objNameSpace needs to access the iCloud instance of the folder
Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar)
'Found this code in a forum somewhere and it does appear to find the iCloud folder
folderName = "iCloud"
Set FoundFouder = FindInFolders(objOutlook.Session.Folders, folderName)
If Not FoundFouder Is Nothing Then
Set objOutlook.ActiveExplorer.CurrentFolder = FoundFouder
Else
MsgBox "Folder: " & folderName & " Not Found", vbInformation
End If
'*** I have no idea how to get from the found folder to the creation of objCalendarItems.
'This next one should be fine, once we have the correct calendar
Set objCalendarItems = objCalendar.Items
'The following code should work IF the correct folder/calendar is pointed to!!
Set curdb = CurrentDb
strWhereCond = "SELECT * FROM Entertainment WHERE ReferenceNo = " & Me!ReferenceNo
Set entertainmentRst = curdb.OpenRecordset(strWhereCond)
Do While Not entertainmentRst.EOF
' There is one entertainment record for each booking day of an event
strWhere = "[Reference] = " & Me!ReferenceNo & _
" AND [Line] = " & entertainmentRst!LineNo
chrStartDateTime = FormatDateTime(entertainmentRst!ShowDate, vbShortDate) & " " & _
FormatDateTime(entertainmentRst!StartTime, vbLongTime)
chrEndDateTime = FormatDateTime(entertainmentRst!ShowDate, vbShortDate) & " " & _
FormatDateTime(entertainmentRst!EndTime, vbLongTime)
Set objAppointment = objCalendarItems.Find(strWhere)
doAppointment = True
If Me!BookingStatus = "CAN" And Not objAppointment Is Nothing Then
With objAppointment
.Delete
End With
doAppointment = False
End If
If entertainmentRst!ShowDate < Now - 7 Then
doAppointment = False
End If
If objAppointment Is Nothing And doAppointment = True Then
' Create the appointment
Set objAppointment = objCalendar.Items.Add(olAppointmentItem)
With objAppointment
Set objOutlookProperties = objAppointment.UserProperties
Set objAppointmentProperty = objOutlookProperties.Add("Reference", olNumber, True, 1)
objAppointmentProperty.Value = Me!ReferenceNo
Set objAppointmentProperty = objOutlookProperties.Add("Line", olNumber, True, 1)
objAppointmentProperty.Value = entertainmentRst!LineNo
.Save
End With
End If
' There's a lot more stuff beyond this, but it's just setting up the notes section of the calendar entry.
Unfortunately, this means that there is no longer any data calendar or contact data kept in the default Outlook folder. I need to change the coding to select this different folder that iCloud uses.
It appears that the folder is seen by the VBA code, but I am missing the key part to actually create the object here.
The relevant code currently in use follows. I have heavily annotated the code to indicate where I am digressing from the working code.
Any assistance would be greatly appreciated.
Private Sub updateOutlookAppointment()
Dim objOutlook As New Outlook.Application
Dim objCalendar As MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objCalendarItems As Outlook.Items
Dim objAppointment As Outlook.AppointmentItem
Dim folderName As String
Dim foundFolder As Folder
' The next two lines should be fine because they deal with higher level objects
Set objOutlook = CreateObject("Outlook.Application")
Set objAppointment = objOutlook.CreateItem(olAppointmentItem)
' OK, not sure about the next line in the iCloud side of things
Set objNameSpace = objOutlook.GetNamespace("MAPI")
' The problem starts here. objNameSpace needs to access the iCloud instance of the folder
Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar)
'Found this code in a forum somewhere and it does appear to find the iCloud folder
folderName = "iCloud"
Set FoundFouder = FindInFolders(objOutlook.Session.Folders, folderName)
If Not FoundFouder Is Nothing Then
Set objOutlook.ActiveExplorer.CurrentFolder = FoundFouder
Else
MsgBox "Folder: " & folderName & " Not Found", vbInformation
End If
'*** I have no idea how to get from the found folder to the creation of objCalendarItems.
'This next one should be fine, once we have the correct calendar
Set objCalendarItems = objCalendar.Items
'The following code should work IF the correct folder/calendar is pointed to!!
Set curdb = CurrentDb
strWhereCond = "SELECT * FROM Entertainment WHERE ReferenceNo = " & Me!ReferenceNo
Set entertainmentRst = curdb.OpenRecordset(strWhereCond)
Do While Not entertainmentRst.EOF
' There is one entertainment record for each booking day of an event
strWhere = "[Reference] = " & Me!ReferenceNo & _
" AND [Line] = " & entertainmentRst!LineNo
chrStartDateTime = FormatDateTime(entertainmentRst!ShowDate, vbShortDate) & " " & _
FormatDateTime(entertainmentRst!StartTime, vbLongTime)
chrEndDateTime = FormatDateTime(entertainmentRst!ShowDate, vbShortDate) & " " & _
FormatDateTime(entertainmentRst!EndTime, vbLongTime)
Set objAppointment = objCalendarItems.Find(strWhere)
doAppointment = True
If Me!BookingStatus = "CAN" And Not objAppointment Is Nothing Then
With objAppointment
.Delete
End With
doAppointment = False
End If
If entertainmentRst!ShowDate < Now - 7 Then
doAppointment = False
End If
If objAppointment Is Nothing And doAppointment = True Then
' Create the appointment
Set objAppointment = objCalendar.Items.Add(olAppointmentItem)
With objAppointment
Set objOutlookProperties = objAppointment.UserProperties
Set objAppointmentProperty = objOutlookProperties.Add("Reference", olNumber, True, 1)
objAppointmentProperty.Value = Me!ReferenceNo
Set objAppointmentProperty = objOutlookProperties.Add("Line", olNumber, True, 1)
objAppointmentProperty.Value = entertainmentRst!LineNo
.Save
End With
End If
' There's a lot more stuff beyond this, but it's just setting up the notes section of the calendar entry.