Moving appointments from the default to a target calendar.
-
Thursday, February 23, 2012 7:25 PM
Hi,
I posted a related question about storing events in a custom calendar a couple of days back. Based on the useful replies I got and after further googling, I revisited the code. The sub shown below works (ie source events are correctly displayed) but the appointments do not appear in the target calendar. I believe the target calendar's identifier (StoreID) must be correct since the associated folder path points to the correct hierarchy. (For info, the code executes via automation from MS Access. )
Problems:
1. As indicated above, the appointments won't show in the target calendar (Hotmail), though they are removed from the source calendar.
2. Also, only a fraction of the appointments (n-2, n-3,..) are moved (ie deleted) from the src calendar. The remaining appts are deleted on subsequent calls. Why?
3. (Advanced). Currently, the code selects all source calendar appointments. Is it possible to modify the code to select only those appointments that occur within a certain period?
Can someone provide recommendations on how to solve these issues? TIA.Private Sub ListOLFolderItems(strDestStoreID As String) ' Copies appts from the default to the target calendar, referenced to by the StoreID. Dim oApp As Outlook.Application, oNsp As Outlook.NameSpace, objSrcFolder As Outlook.Folder, objTgtFolder As Outlook.Folder, colSyc As Outlook.SyncObjects Dim colSrcItems As Outlook.Items, ii As Integer, objAppt As Outlook.AppointmentItem On Error GoTo ListOLFolderItems_Err Set oApp = CreateObject("outlook.application") Set oNsp = oApp.Application.GetNamespace("MAPI") Set colSyc = oNsp.SyncObjects Set objSrcFolder = oNsp.GetDefaultFolder(Outlook.olFolderCalendar) ' Src = default calendar Set objTgtFolder = oNsp.GetFolderFromID(strDestStoreID) ' Tgt: Hotmail calendar Debug.Print "Src StoreID: " & vbCrLf & objSrcFolder.StoreID & vbCrLf Debug.Print "Src FolderPath: " & objSrcFolder.FolderPath ' ie "\\Outlook Data File\Calendar" Debug.Print "Tgt StoreID: " & vbCrLf & objTgtFolder.StoreID & vbCrLf Debug.Print "Tgt FolderPath: " & objTgtFolder.FolderPath ' ie "\\Hotmail" Debug.Print "" Set colSrcItems = objSrcFolder.Items ii = 1 For Each objAppt In colSrcItems objAppt.Move objTgtFolder Debug.Print ii, Format(objAppt.CreationTime, "dd.mm.yyyy hh:nn"), objAppt.Subject ii = ii + 1 Next ListOLFolderItems_Exit: Set oApp = Nothing: Set oNsp = Nothing: Set objSrcFolder = Nothing: Set objTgtFolder = Nothing Set colSrcItems = Nothing: Set objAppt = Nothing Exit Sub ListOLFolderItems_Err: MsgBox "Error " & Err.Number & " (" & Err.Description & ") in ListOLFolderItems()" 'Array index out of bounds. Resume ListOLFolderItems_Exit End Sub
- Edited by Helveticus Thursday, February 23, 2012 7:27 PM
- Edited by Helveticus Thursday, February 23, 2012 7:28 PM
- Edited by Helveticus Thursday, February 23, 2012 7:31 PM
- Edited by Helveticus Thursday, February 23, 2012 7:31 PM
- Edited by Helveticus Thursday, February 23, 2012 7:32 PM
- Edited by Helveticus Thursday, February 23, 2012 7:32 PM
- Edited by Helveticus Thursday, February 23, 2012 7:33 PM
- Edited by Helveticus Thursday, February 23, 2012 9:16 PM
- Edited by Helveticus Thursday, February 23, 2012 9:16 PM
- Edited by Helveticus Thursday, February 23, 2012 9:22 PM
- Edited by Helveticus Thursday, February 23, 2012 9:24 PM
- Edited by Helveticus Thursday, February 23, 2012 9:25 PM
- Edited by Helveticus Thursday, February 23, 2012 9:30 PM
- Edited by Helveticus Thursday, February 23, 2012 9:31 PM
- Edited by Helveticus Thursday, February 23, 2012 9:31 PM
All Replies
-
Friday, February 24, 2012 2:22 PM
Firstly, do not use foreach when modifying the collection. Use a down loop. Secondly, Move is a function returning the new item. After calling Move, the old item must be immediately discarded
for i = colSrcItems.Count to 1 step -1
set objAppt = colSrcItems.Item(i)
set objAppt = objAppt.Move(objTgtFolder)
Debug.Print ii, Format(objAppt.CreationTime, "dd.mm.yyyy hh:nn"), objAppt.Subject
ii = ii + 1
NextDmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.2 is now available!
- Marked As Answer by Helveticus Friday, February 24, 2012 4:38 PM
-
Friday, February 24, 2012 4:45 PM
Thanks for your suggestion, Dimitry.
I made the modification. Incidentally, the reason why the appointments did not show up in the target calendar was related to a folder issue. The target calendar did point to a non update-able object, contrary to what I had checked. (Found out by attempting to list all target calendar items prior to moving the new appointments. The StoreID, EntryID and FolderPath data are too cryptic .
Thanks again!
- Edited by Helveticus Friday, February 24, 2012 4:46 PM
- Edited by Helveticus Friday, February 24, 2012 4:46 PM
- Edited by Helveticus Friday, February 24, 2012 4:48 PM
- Edited by Helveticus Friday, February 24, 2012 4:49 PM

