Outlook应用程序 - 无法获取文件夹列表

时间:2011-11-15 11:20:50

标签: vb.net outlook office-interop directory

我创建了一个小程序来将我的电子邮件保存到Windows文件夹。

我无法检索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

1 个答案:

答案 0 :(得分:0)

这是我刚才写的代码。它将文件夹添加到树视图中:

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

它可能不完美,但对我有用: - )