I would like to move Appointment items, both from msg files and from a PST, to a new separate PST. However I continually get errors.
Here is my latest attempt for moving msg files, my code is similar with PSTs, except the Item is created from a folder loop of the PST. ReleaseObj class is just a class that checks if an object is null, and if not calls Marshal.ReleaseComObject:
Stores stores = null;
Store currStore = null;
Folder newPSTRootFolder = null;
Folder msgAddFolder = null;
Folder appointmentAddFolder = null;
try
{
outlookNs = app.Session;
outlookNs.AddStore(newPSTFile);
//Create Calendar folder for appointments
appointmentAddFolder = outlookNs.GetDefaultFolder(OlDefaultFolders.olFolderCalendar) as Folder;
stores = outlookNs.Stores;
for (int i = 1; i <= stores.Count; i++)
{
currStore = stores[i];
if (currStore.FilePath == newPSTFile)
{
newPSTRootFolder = (Folder)currStore.GetRootFolder();
msgAddFolder = newPSTRootFolder.Folders.Add(customFolder, Type.Missing) as Folder;
}
ReleaseObj(currStore);
}
string[] msgFilesPull = Directory.GetFiles(msgFolder, "*.msg", SearchOption.AllDirectories);
foreach(string msgFile in msgFilesPull)
{
try
{
var item = app.Session.OpenSharedItem(msgFile);
if (item is MailItem mailItem)
{
mailItem.Move(msgAddFolder as Folder);
ReleaseObj(item);
ReleaseObj(mailItem);
}
else if (item is ContactItem contactItem)
{
contactItem.Move(msgAddFolder as Folder);
ReleaseObj(item);
ReleaseObj(contactItem);
}
else if (item is AppointmentItem appointmentItem)
{
appointmentItem.Move(appointmentAddFolder as Folder);
ReleaseObj(item);
ReleaseObj(appointmentItem);
}
else if (item is TaskItem taskItem)
{
taskItem.Move(msgAddFolder as Folder);
ReleaseObj(item);
ReleaseObj(taskItem);
}
else if (item is MeetingItem meetingItem)
{
meetingItem.Move(msgAddFolder as Folder);
ReleaseObj(item);
ReleaseObj(meetingItem);
}
ReleaseObj(item);
}
catch (System.Exception ex)
{
MessageBox.Show($"ERROR\nCould not add {msgFile}\n{ex}");
}
}
}
The above code fails to adds the Appointment, with a generic "COMException(0x80040201): The Operation Failed.
I have also tried just adding the Appointment to the newly created msgAddFolder, but get an error that the folder is the wrong type.
Is there any way I can actually accomplish this?