使用VBA在Outlook 2003中循环通过PST

时间:2011-11-07 19:59:21

标签: vba outlook outlook-vba outlook-2003 pst

在Outlook 2007中,我可以使用以下代码遍历邮件存储,包括PST:

Dim stores As Outlook.stores
Set stores = objNamespace.stores
Dim store As Outlook.store

For Each store In stores
    MsgBox store.FilePath
Next

但是,在Outlook 2003中,Outlook.store和Outlook.stores对象不存在。

Outlook 2003中是否有等效的对象? 我可以用什么方法来遍历邮件存储?

谢谢。

1 个答案:

答案 0 :(得分:4)

此Outlook 2003示例代码将循环遍历高级邮箱,并将某些属性打印到立即窗口。我根据您的要求选择了最有用的属性。

Sub LoopThruMailboxes()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim mailboxCount As Long
Dim i As Long
Dim folder As Outlook.MAPIFolder

' get local namespace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")

mailboxCount = olNS.Folders.count

For i = 1 To mailboxCount
  Set folder = olNS.Folders(i)

  Debug.Print folder.EntryID
  Debug.Print folder.StoreID
  Debug.Print folder.Name
  Debug.Print folder.FolderPath
Next i

End Sub

folder.Name 是邮箱的名称, folder.StoreID 是商店ID(我不确定你的“商店文件路径”是什么意思,我没有看到任何看起来相关的东西)。

这是一个功能化版本,它将文件夹名称和商店ID作为数组返回,您可以直接将其分配给列表框:

Function GetMailBoxInfo() As String()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim mailboxCount As Long
Dim i As Long
Dim folder As Outlook.MAPIFolder
Dim tempString() As String

' get local namespace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")

mailboxCount = olNS.Folders.count

' size array accordingly
ReDim tempString(1 To mailboxCount, 1 To 2)

For i = 1 To mailboxCount
  Set folder = olNS.Folders(i)

  tempString(i, 1) = folder.Name
  tempString(i, 2) = folder.StoreID
Next i

  GetMailBoxInfo = tempString

End Function

例如:

ListBox1.List = GetMailBoxInfo