2016-07-28 36 views
0

我試圖以編程方式更改重複項目(並使例外)。在Outlook加載項中保存例外

該項目是一個Outlook 2010 AddIn。

我試着下面的代碼,但一對夫婦後,保存在calitm.Save()命令

extracted ="somelocation" 

//that's a fancy way to iterate on a list of appointment items 
for (int i = 0; i < filterAppointmentsToChangeLocation.RecordCount; i++) 
{ 
    int selrow = 1 
    var calitm = filterAppointmentsToChangeLocation.data[selrow].GetOlAppointment(); 
    //this returns an appointmentitem that is associated with a form 
    //that contains the location property 

    calitm.UserProperties["location"].Value = extracted; 
    calitm.Save(); 

    Marshal.ReleaseComObject(calitm); 
} 

你有什麼建議退出代碼? thnx你的時間...

回答

0

如果你的代碼存在,這意味着在你的Save方法調用過程中,某些東西正在崩潰。

你真正需要的try/catch您的斷層代碼,以便您可以保存/重新引發你的異常

for (int i = 0; i < filterAppointmentsToChangeLocation.RecordCount; i++) 
{ 
    int selrow = 1 
    var calitm = filterAppointmentsToChangeLocation.data[selrow].GetOlAppointment(); 

    try 
    { 
     calitm.UserProperties["location"].Value = extracted; 
     calitm.Save(); 
    } 
    catch (Exception e) 
    { 
     // save, output or retrhow your exception. 
     System.IO.File.WriteAllText (@"C:\somepath\error.txt", e.Message); 
    } 
    Marshal.ReleaseComObject(calitm); 
} 
+0

你什麼意思保存,輸出或再次引發你的異常。每種方法會做什麼?我可以重試保存嗎?保存應該在每個項目上執行是相當重要的 –

+0

看看這裏http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice看看用異常處理你可以做什麼;您可以嘗試重播您的保存,或通過重新拋出異常來停止for循環 – bsoulier