2011-11-15 54 views
0

我創建了一個小程序將我的電子郵件保存到Windows文件夾。Outlook應用程序 - 無法獲取文件夾列表

我無法檢索Outlook文件夾列表。

當我檢查我是否選擇了收件箱文件夾,它不是!

Dim app As Outlook.Application 
Dim ns As Outlook.NameSpace 
Dim siFolder As Outlook.Folder 

Dim Annee As Integer 
Dim Mois As Integer 
Dim Jour As Integer 

app = New Outlook.Application() 
ns = app.Session 

siFolder = CType(ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox), Outlook.Folder) 'I select here the Inbox Folder 

' I do a test, but siFolder.name appears not to be the inbox folder !! 

If siFolder.Name Is ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) Then 
    Annee = Format(mailitem.ReceivedTime, "yyyy") 
    Mois = Format(mailitem.ReceivedTime, "MM") 
    Jour = Format(mailitem.ReceivedTime, "dd") 
ElseIf siFolder Is ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) Then 
    Annee = Format(mailitem.SentOn, "yyyy") 
    Mois = Format(mailitem.SentOn, "MM") 
    Jour = Format(mailitem.SentOn, "dd") 
Else 
    MsgBox("Aucune boite sélectionnée") 
    End 'because i can't select the Inbox, application ends here. 
End If 
+0

創造了VB的對象引用時,不要你需要使用「設置」關鍵詞。淨?它應該是'** Set ** app =新Outlook.Application'和'** Set ** ns = app.Session' – JimmyPena

回答

0

這是我前一段時間寫的代碼。它增加了文件夾到一個TreeView:

Public Sub startTreeviewBuild() 

    Dim oApp As New Outlook.Application() 
    Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi") 
    Dim objFolder As Outlook.MAPIFolder = Nothing 

    oNS.Logon("Outlook", "", False, True) 
    'oNS.Session.AddStore("C:\pstfile.pst")' uncomment to point to a pst file 
    objFolder = oNS.Session.Folders.GetLast() 

    TreeView1.Visible = False 
    TreeView1.Nodes.Clear() 
    TreeView1.Nodes.Add(objFolder.Name) 
    buildPSTTreeview(objFolder, TreeView1.Nodes(0)) 
    TreeView1.Visible = True 

End Sub 

Public Sub buildPSTTreeview(ByVal StartFolder As Outlook.MAPIFolder, ByVal parentnode As TreeNode) 
    Dim objItem As Object 
    Dim mailItem As Outlook.MailItem 
    Dim MItem As Outlook.MailItem = Nothing 

    For i = 1 To StartFolder.Items.Count 
     objItem = StartFolder.Items.Item(i) 
     If TryCast(objItem, Outlook.MailItem) IsNot Nothing Then 
      mailItem = DirectCast(objItem, Outlook.MailItem) 
      If mailItem.Subject = Nothing Then 
       parentnode.Nodes.Add(mailItem.EntryID) 
      Else 
       parentnode.Nodes.Add(mailItem.Subject.ToString) 
      End If 

     End If 
    Next 

    ' process all the subfolders of this folder 
    Dim objFolder As Outlook.MAPIFolder = Nothing 
    For i = 1 To StartFolder.Folders.Count 
     objFolder = StartFolder.Folders.Item(i) 
     Dim myNode As TreeNode = New TreeNode(objFolder.Name) 
     parentnode.Nodes.Add(myNode) 
     'objFolder.Name 
     Application.DoEvents() 
     Call buildPSTTreeview(objFolder, myNode) 
    Next 
    objFolder = Nothing 
    objItem = Nothing 

End Sub 

它可能並不完美,butit工作對我來說:-)

+0

謝謝David, 這非常有幫助。 你能告訴我,如果有辦法: 1.-選擇只包含電子郵件(不包括聯繫人,日曆,任務,...)的文件夾來填充樹視圖? 2.-選擇其他個人文件夾或類似[email protected]的文件夾([email protected]也包含一些文件夾,如:收件箱,sentitems,...)。 非常感謝! – user1047402

+0

@joran:你好Joran,你有解決方案選擇Outlook中的文件夾並填充樹視圖嗎? (個人文件夾,檔案文件夾,...)?謝謝 – user1047402

相關問題