2012-04-03 111 views
1

我一直試圖找到一種方法,一個文件夾從C#從Outlook在C#中添加文件夾到PST文件

添加到PST文件

我已經嘗試了一大堆代碼,試圖讓這工作,這是似乎是最likly是正確的一個(因爲它是在MSDN什麼),但仍然無法正常工作

Main { 

Outlook._Application OutlookObject = new Outlook.Application(); 
      Outlook.Store NewPst = null; 
      // create the pst file 
      string pstlocation = "C:\\Users\\Test\\Desktop\\PST\\Test.pst"; 
      try 
     { 
      OutlookObject.Session.AddStore(pstlocation); 

      foreach (Outlook.Store store in OutlookObject.Session.Stores) 
      { 
       if (store.FilePath == pstlocation) 
       { 
        // now have a referance to the new pst file 
        NewPst = store; 
        Console.WriteLine("The Pst has been created"); 
       } 
      } 
     } 
     catch 
     { } 
     // create a folder or subfoler in pst 
     Outlook.MAPIFolder NewFolder; 

     NewFolder = NewPst.Session.Folders.Add("New Test Folder", Type.Missing); 
} 

這段代碼創建一個新的PST文件,然後改掉添加一個文件夾但是最後一行代碼:

New NewFolder = NewPst.Session.Folders.Add("New Test Folder", Type.Missing); 

獲取錯誤「操作失敗。」和「無效的轉換異常」能有人指出我所事先做錯了

感謝

回答

4

您需要使用Store.GetRootFolder()獲得的句柄存儲的根文件夾Store.Session )。所以,你可以使用:

// create a folder or subfolder in pst  
Outlook.MAPIFolder pstRootFolder = NewPst.GetRootFolder(); 
Outlook.MAPIFolder NewFolder = pstRootFolder.Folders.Add("New Test Folder"); 

我建議書籤以下兩項:PIA的文檔並不總是完整的,所以這是值得一試的COM文件以及完整類和成員的信息。

+0

非常感謝您的幫助這解決了我的問題 – jgok222 2012-04-03 14:20:01

相關問題