2016-07-28 37 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