2017-04-19 89 views
0

這裏的問題是:如何在Outlook 2010中獲得最新的聯繫人圖片?

  1. 使用Attachment.SaveAsFile()來保存聯繫人的圖片在磁盤上(成功)
  2. 手動更改Outlook聯繫人的圖片。
  3. 重複步驟1,但我得到了老照片,而不是新的步驟2.

編輯: 我已經知道如何保存聯繫人的圖片在磁盤上。 問題是,我得到的圖片不是最新的。 這裏是代碼:

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
     Outlook.Application outlook = new Outlook.Application(); 
     Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
     foreach (var item in folder.Items) { 
      if (item is Outlook.ContactItem) { 
       Outlook.ContactItem contact = null; 
       Outlook.Attachments atts = null; 
       Outlook.Attachment att = null; 
       string path = ""; 

       contact = item as Outlook.ContactItem; 

       if (!contact.HasPicture) { continue; } 

       path = @"C:\Temp\" + contact.EntryID + ".jpg"; 
       atts = contact.Attachments; 
       att = atts["ContactPicture.jpg"]; 

       if(File.Exists(path)){ 
        File.Delete(path); 
       } 

       att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg"); 

       Marshal.ReleaseComObject(att); 
       att = null; 
       Marshal.ReleaseComObject(atts); 
       atts = null; 
       Marshal.ReleaseComObject(contact); 
       contact = null; 
      } 
     } 

謝謝!

+0

[so] is * not * a free code writing service。預計你會嘗試**自己編寫代碼**。在[做更多研究]之後(http://meta.stackoverflow.com/questions/261592),如果你有問題,你可以**發佈你已經嘗試過**的清單,說明什麼是不工作的**並提供** [mcve] **。我建議閱讀[問]一個好問題和[完美問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要參加[旅遊]。 – Smartis

+0

確保聯繫人已保存 –

+0

@Smartis感謝您的回覆。也許我沒有描述清楚。我不希望有人爲我寫代碼。 – cocoa

回答

0

我找到Contact picture retrieved via PIA doesn't change when updated in Outlook

分辨率沒釋放folder.Items

下面是修改代碼:

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
     Outlook.Application outlook = new Outlook.Application(); 
     Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
     Outlook.Items items = null; 
     items = folder.Items; 
     foreach (var item in items) { 
      if (item is Outlook.ContactItem) { 
       Outlook.ContactItem contact = null; 
       Outlook.Attachments atts = null; 
       Outlook.Attachment att = null; 
       string path = ""; 

       contact = item as Outlook.ContactItem; 

       if (!contact.HasPicture) { continue; } 

       path = @"C:\Temp\" + contact.EntryID + ".jpg"; 
       atts = contact.Attachments; 
       att = atts["ContactPicture.jpg"]; 

       if (File.Exists(path)) { 
        File.Delete(path); 
       } 

       att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg"); 

       Marshal.ReleaseComObject(att); 
       att = null; 
       Marshal.ReleaseComObject(atts); 
       atts = null; 
       Marshal.ReleaseComObject(contact); 
       contact = null; 
      } 
     } 

     if (items != null) { 
      Marshal.ReleaseComObject(items); 
      items = null; 
     } 

編輯:

發生問題與上面的代碼。有時它得到了舊圖片。 我在最後添加了GC.Collect();,然後運行良好。

所以我試了下面的代碼。但問題依然存在。

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
     Outlook.Application outlook = new Outlook.Application(); 
     Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
     Outlook.Items items = folder.Items; 

     foreach (var item in items) { 
      if (item is Outlook.ContactItem) { 
       Outlook.ContactItem contact = item as Outlook.ContactItem; 
       string path = ""; 
       Outlook.Attachment att = null; 

       if (!contact.HasPicture) { continue; } 

       path = @"C:\Temp\" + contact.EntryID + ".jpg"; 
       att = contact.Attachments["ContactPicture.jpg"]; 

       if (File.Exists(path)) { 
        File.Delete(path); 
       } 

       att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg"); 
      } 
     } 

     GC.Collect(); 

最後我使用下面的代碼。釋放所有com對象,然後調用GC.Collect()。

//[Outlook] is short for [Microsoft.Office.Interop.Outlook] 
     Outlook.Application outlook = new Outlook.Application(); 
     Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
     Outlook.Items items = folder.Items; 

     foreach (var item in items) { 
      if (item is Outlook.ContactItem) { 
       Outlook.ContactItem contact = null; 
       Outlook.Attachments atts = null; 
       Outlook.Attachment att = null; 
       string path = ""; 

       contact = item as Outlook.ContactItem; 

       if (!contact.HasPicture) { continue; } 

       path = @"C:\Temp\" + contact.EntryID + ".jpg"; 
       atts = contact.Attachments; 
       att = atts["ContactPicture.jpg"]; 

       if (File.Exists(path)) { 
        File.Delete(path); 
       } 

       att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg"); 

       Marshal.ReleaseComObject(att); 
       att = null; 
       Marshal.ReleaseComObject(atts); 
       atts = null; 
       Marshal.ReleaseComObject(contact); 
       contact = null; 
      } 
     } 

     if (items != null) { 
      Marshal.ReleaseComObject(items); 
      items = null; 
     } 

     Marshal.ReleaseComObject(folder); 
     folder = null; 

     Marshal.ReleaseComObject(outlook); 
     outlook = null; 

     GC.Collect();