问题
I'm trying to save a appointment series subject. This works as expected but after the save, When I performed move/edit
the calendar item, I get this error.
Code to reproduce error.
public void OnMyButtonClickContext(Office.IRibbonControl control)
{
var sel = control.Context as Microsoft.Office.Interop.Outlook.Selection;
var i = sel[1] as Microsoft.Office.Interop.Outlook.AppointmentItem;
i.Parent.Subject = i.Parent.Subject + " [CONFIRMED]";
i.Parent.Save();
}
I've tried setting i to null, using Marhsal.ReleaseComObject(i)
. Neither of which seems to help.
回答1:
I don't see any release COM objects statements in the code. What objects exactly did you try to release?
Why do you always use the Parent property of the AppointmentItem class?
Each time you call the Parent property you get the reference counter increased. And then you need to release such objects in the code.
Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.
Here is what MSDN states for 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 object, and any 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.
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.
Looks like you didn't release all underlying COM objects in the code.
来源:https://stackoverflow.com/questions/36524084/outlook-vsto-save-appointmentitem-parent