PDA

View Full Version : Modifying recurring appointments: Outlook 365 vs Outlook 2003



gautxori
08-16-2019, 04:29 AM
Hi,

I have some code to modify start dates for appointments depending on whether they are recurring or not, this was coded for Outlook 2003:


(txtTiempo, cmbUdTiempo, cmbSensitivity, cmbFlexibilidad are a textBox and Combo Boxes in a Form; optAlta, optNormal and optBaja are Radio Buttons)

Private m_ApptsSelection As Selection

Private Sub cmdActualizar_Click()
Dim myAppt As AppointmentItem, bChanged, importance
Set m_ApptsSelection = Outlook.Application.ActiveExplorer.Selection
For Each myAppt In m_ApptsSelection
bChanged = False
If txtTiempo.Value * 1 <> 0 Then bChanged = True
If Not myAppt.IsRecurring Then
myAppt.Start = DateAdd(cmbUdTiempo.Value, txtTiempo.Value * 1, myAppt.Start)
Else
myAppt.GetRecurrencePattern.PatternStartDate = DateAdd(cmbUdTiempo.Value, txtTiempo.Value * 1, myAppt.GetRecurrencePattern.PatternStartDate)
End If
If myAppt.Sensitivity <> cmbSensitivity.Value Then myAppt.Sensitivity = cmbSensitivity.Value: bChanged = True
If myAppt.BusyStatus <> cmbFlexibilidad.Value Then myAppt.BusyStatus = cmbFlexibilidad.Value: bChanged = True
importance = (optAlta And olImportanceHigh) Or (optNormal And olImportanceNormal) Or (optBaja And olImportanceLow)
If myAppt.importance <> importance Then myAppt.importance = importance: bChanged = True
If bChanged Then myAppt.Save
Next
Set myAppt = Nothing
End Sub

This worked perfectly in Outlook 2003 for both recurring and non recurring appointments. Does not in Outlook 365, for recurring ones.
I get this error message:
24826
and when debugging, i inmediately get a second error message:
24828
Trying to find out why I read in Office help that


When you work with recurring appointment items, you should release any prior references, obtain new references to the recurring appointment item before you access or modify the item, and release these references as soon as you are finished and have saved the changes. This practice applies to the recurring AppointmentItem (https://docs.microsoft.com/en-us/office/vba/api/outlook.appointmentitem) object, and any Exception (https://docs.microsoft.com/en-us/office/vba/api/outlook.exception) or RecurrencePattern object. To release a reference in Visual Basic for Applications (VBA) or Visual Basic, set that existing object to Nothing. In C#, explicitly release the memory for that object. For a code example, see the topic for the AppointmentItem object.
Note that even after you release your reference and attempt to obtain a new reference, if there is still an active reference, held by another add-in or Outlook, to one of the above objects, your new reference will still point to an out-of-date copy of the object. Therefore, it is important that you release your references as soon as you are finished with the recurring appointment.


I suppose that might be the clue, I tried to update code to this:


Private m_ApptsSelection As Selection

Private Sub cmdActualizar_Click()
Dim myAppt As AppointmentItem, bChanged, importance, icSel
Dim ApptRecPatt As RecurrencePattern
Set m_ApptsSelection = Outlook.Application.ActiveExplorer.Selection
For icSel = 1 To m_ApptsSelection.Count
Set myAppt = m_ApptsSelection.item(icSel)
bChanged = False
If txtTiempo.Value * 1 <> 0 Then bChanged = True
If Not myAppt.IsRecurring Then
myAppt.Start = DateAdd(cmbUdTiempo.Value, txtTiempo.Value * 1, myAppt.Start)
Else
Set ApptRecPatt = myAppt.GetRecurrencePattern
Set myAppt = Nothing
If ApptRecPatt.Exceptions.Count > 0 Then
Stop ' TO-DO: update exceptions
End If
ApptRecPatt.PatternStartDate = DateAdd(cmbUdTiempo.value, txtTiempo.value * 1, ApptRecPatt.PatternStartDate)
Set myAppt = m_ApptsSelection.item(icSel)
'myAppt.Save: DOES NOT WORK, I GET AN ERROR.
End If
If myAppt.Sensitivity <> cmbSensitivity.Value Then myAppt.Sensitivity = cmbSensitivity.Value: bChanged = True
If myAppt.BusyStatus <> cmbFlexibilidad.Value Then myAppt.BusyStatus = cmbFlexibilidad.Value: bChanged = True
importance = (optAlta And olImportanceHigh) Or (optNormal And olImportanceNormal) Or (optBaja And olImportanceLow)
If myAppt.importance <> importance Then myAppt.importance = importance: bChanged = True
If bChanged Then myAppt.Save ' DOES NOT WORK, I GET AN ERROR.
Next
Set myAppt = Nothing
End Sub

marked instructions do not work. I want it to work for Outlook 365. Can anyone help to fix this? Thanks.

gautxori
08-16-2019, 08:51 AM
My..., I found the solution: M$, once again messing and creating problems where there was none.
They figured there should be a Master Appointment, and occurrences would become child items from the master one... and Exceptions are child items too... "overpopulation", i'd call that (i see no point in creating a "master recurring appointment" and "child occurrences" as long as there were already the exceptions hanging around, but whatever).

Here's the new code, in case someone finds it useful:


Private m_ApptsSelection As Selection

Private Sub cmdActualizar_Click()
Dim myAppt As AppointmentItem, bChanged, importance, icSel
Dim ApptRecPatt As RecurrencePattern
Set m_ApptsSelection = Outlook.Application.ActiveExplorer.Selection
For icSel = 1 To m_ApptsSelection.Count
Set myAppt = m_ApptsSelection.item(icSel)
bChanged = False
If txtTiempo.Value * 1 <> 0 Then bChanged = True
If Not myAppt.IsRecurring Then
myAppt.Start = DateAdd(cmbUdTiempo.Value, txtTiempo.Value * 1, myAppt.Start)
Else
Set ApptRecPatt = myAppt.GetRecurrencePattern
Set myAppt = myAppt.GetRecurrencePattern.Parent
If ApptRecPatt.Exceptions.Count > 0 Then
Stop ' TO-DO: update exceptions
End If
ApptRecPatt.PatternStartDate = DateAdd(cmbUdTiempo.value, txtTiempo.value * 1, ApptRecPatt.PatternStartDate)
End If
If myAppt.Sensitivity <> cmbSensitivity.Value Then myAppt.Sensitivity = cmbSensitivity.Value: bChanged = True
If myAppt.BusyStatus <> cmbFlexibilidad.Value Then myAppt.BusyStatus = cmbFlexibilidad.Value: bChanged = True
importance = (optAlta And olImportanceHigh) Or (optNormal And olImportanceNormal) Or (optBaja And olImportanceLow)
If myAppt.importance <> importance Then myAppt.importance = importance: bChanged = True
If bChanged Then myAppt.Save
Next
Set myAppt = Nothing
End Sub
.

BTW, i largely miss the Week View in Outlook, the one we had in Outlook 2003. Does someone know of a plugin to get such a nice view?