Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Outlook calendar items in an iCloud folder

Status
Not open for further replies.

theConjurian

IS-IT--Management
May 4, 2002
35
0
0
CA
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.
 
' The problem starts here. objNameSpace needs to access the iCloud instance of the folder
Set [red]objCalendar[/red] = objNameSpace.GetDefaultFolder(olFolderCalendar)

'Found this code in a forum somewhere and it does appear to find the iCloud folder

folderName = "iCloud"
Set [red]FoundFouder[/red] = FindInFolders(objOutlook.Session.Folders, folderName)

Notice how the red objects are not the same and therefore not a fix to your code.
Obviously you will need to comment out the original Outlook version.
 
I missed the typo error you mentioned, but I have made the change and reduced the code area to the following:

Set objNameSpace = objOutlook.GetNamespace("MAPI")

folderName = "iCloud"
Set foundFolder = FindInFolders(objOutlook.Session.Folders, folderName)

If Not foundFolder Is Nothing Then
Set objOutlook.ActiveExplorer.CurrentFolder = foundFolder
Set objCalendarItems = objOutlook.ActiveExplorer.CurrentFolder.Items
Else
MsgBox "Folder: " & folderName & " Not Found", vbInformation
End If

' At this point objCalendarItems should be pointing to the \\iCloud folder

Set curdb = CurrentDb

strWhereCond = "SELECT * FROM Entertainment WHERE ReferenceNo = " & Me!ReferenceNo
Set entertainmentRst = curdb.OpenRecordset(strWhereCond)

Do While Not entertainmentRst.EOF

strWhere = "[Reference] = " & Me!ReferenceNo & _
" AND [Line] = " & entertainmentRst!LineNo

' The program fails at this point as the property 'Reference' is not set

Set objAppointment = objCalendarItems.Find(strWhere)

Any additional insights would be appreciated
 
Having recently written an Outlook Macro and wandering back here, I think I may have more insight now (trial by fire).

My hunch is that all the items in your "folder" may not be of an item type that supports the property. You might try iterating over the contents of the folder and testing the type of item...

Here is a code excerpt that may be useful. In this case, I was looking for the apparent "received date" on items in my inbox (so I could set a user defined field to just the date, so I could in turn group on it). The point being that an NDR does not have a ReceivedTime property, "received date" is something else for that item type.

Code:
  Select Case TypeName(Item)
    Case "ReportItem" 'NDR's - Non Delivery Reports
        dtReceived = Item.CreationTime 'Looks like this is the "Received Date"
    Case Else 'MailItem and MeetingItem I definitely saw working
      dtReceived = Item.ReceivedTime
  End Select

I hope this sends you down a useful path if you did not figure it out yet. If you did, others will appreciate your work later if you post it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top