我很少使用Access進行編程,但是我從Outlook中移動了一些代碼,繞了一下它並且似乎有效。這不是一個解決方案,但它應該告訴你如何訪問你需要的所有信息。
我有一個問題。如果已經打開Outlook,則Set OutApp = CreateObject("Outlook.Application")
和Set OutApp = New Outlook.Application
都不會創建新的Outlook實例。因此,Quit
關閉Outlook是否在宏啓動之前打開它。我建議你在這個問題上發佈一個新問題;我相信有人知道如何判斷Outlook是否已經開放,因此不會退出。
Outlook中的文件夾結構有些尷尬,因爲頂級文件夾的類型爲Folders
,而所有子文件夾的類型爲MAPIFolder
。一旦你過去了,這很簡單。
以下代碼包含功能GetListSortedChildren(ByRef Parent As MAPIFolder) As String
。該函數查找Parent的所有子項並返回一個字符串,例如「5,2,7,1,3,6,4」,它按名稱按升序列出兒童的索引。我會用這樣的東西來填充ListView,方法是根據用戶需要擴展節點。
我提供了一個子程序CtrlDsplChld()
控制輸出到序列中的所有文件夾的直接窗口。我相信這應該給你足夠的指導,以開始訪問文件夾層次結構。
子程序DsplChld(ByRef Parent As MAPIFolder, ByVal Level As Long)
包括代碼以查找帶有附件的第一消息。這將告訴您如何查看特定消息的文件夾。
最後,選擇CtrlDsplChld()
裏顯示該消息的屬性:主題,要,HTMLBody和附件的顯示名稱。
希望這會有所幫助。
Option Compare Database
Option Explicit
Dim ItemWithMultipleAttachments As Outlook.MailItem
Sub CtrlDsplChld()
Dim ArrChld() As String
Dim ListChld As String
Dim InxAttach As Long
Dim InxChld As Long
Dim InxTopLLCrnt As Long
Dim OutApp As Outlook.Application
Dim TopLvlList As Folders
Set ItemWithMultipleAttachments = Nothing
Set OutApp = CreateObject("Outlook.Application")
'Set OutApp = New Outlook.Application
With OutApp
Set TopLvlList = .GetNamespace("MAPI").Folders
For InxTopLLCrnt = 1 To TopLvlList.Count
' Display top level children and their children
Call DsplChld(TopLvlList.Item(InxTopLLCrnt), 0)
Next
If Not ItemWithMultipleAttachments Is Nothing Then
With ItemWithMultipleAttachments
Debug.Print .Subject
Debug.Print .HTMLBody
Debug.Print .To
For InxAttach = 1 To .Attachments.Count
Debug.Print .Attachments(InxAttach).DisplayName
Next
End With
End If
.Quit
End With
Set OutApp = Nothing
End Sub
Sub DsplChld(ByRef Parent As MAPIFolder, ByVal Level As Long)
Dim ArrChld() As String
Dim InxChld As Long
Dim InxItemCrnt As Long
Dim ListChld As String
Debug.Print Space(Level * 2) & Parent.Name
If ItemWithMultipleAttachments Is Nothing Then
' Look down this folder for a mail item with an attachment
For InxItemCrnt = 1 To Parent.Items.Count
With Parent.Items(InxItemCrnt)
If .Class = olMail Then
If .Attachments.Count > 1 Then
Set ItemWithMultipleAttachments = Parent.Items(InxItemCrnt)
Exit For
End If
End If
End With
Next
End If
ListChld = GetListSortedChildren(Parent)
If ListChld <> "" Then
' Parent has children
ArrChld = Split(ListChld, ",")
For InxChld = LBound(ArrChld) To UBound(ArrChld)
Call DsplChld(Parent.Folders(ArrChld(InxChld)), Level + 1)
Next
End If
End Sub
Function GetListSortedChildren(ByRef Parent As MAPIFolder) As String
' The function returns "" if Parent has no children.
' If the folder has children, the functions returns "P,Q,R, ..." where
' P, Q, R and so on indices of the children of Parent in ascending
' order by name.
Dim ArrInxFolder() As Long
'Dim ArrFolder() As MAPIFolder
Dim InxChldCrnt As Long
Dim InxName As Long
Dim ListChld As String
If Parent.Folders.Count = 0 Then
' No children
GetListSortedChildren = ""
Else
'ReDim ArrName(1 To Parent.Folders.Count)
'For InxChldCrnt = 1 To Parent.Folders.Count
' ArrFolder(InxChldCrnt) = Parent.Folders(InxChldCrnt)
'Next
Call SimpleSortMAPIFolders(Parent, ArrInxFolder)
ListChld = CStr(ArrInxFolder(1))
For InxChldCrnt = 2 To Parent.Folders.Count
ListChld = ListChld & "," & CStr(ArrInxFolder(InxChldCrnt))
Next
GetListSortedChildren = ListChld
End If
End Function
Sub SimpleSortMAPIFolders(ArrFolder As MAPIFolder, _
ByRef InxArray() As Long)
' On exit InxArray contains the indices into ArrFolder sequenced by
' ascending name. The sort is performed by repeated passes of the list
' of indices that swap adjacent entries if the higher come first.
' Not an efficient sort but adequate for short lists.
Dim InxIACrnt As Long
Dim InxIALast As Long
Dim NoSwap As Boolean
Dim TempInt As Long
ReDim InxArray(1 To ArrFolder.Folders.Count) ' One entry per sub folder
' Fill array with indices
For InxIACrnt = 1 To UBound(InxArray)
InxArray(InxIACrnt) = InxIACrnt
Next
If ArrFolder.Folders.Count = 1 Then
' One entry list already sorted
Exit Sub
End If
' Each repeat of the loop moves the folder with the highest name
' to the end of the list. Each repeat checks one less entry.
' Each repeats partially sorts the leading entries and may result
' in the list being sorted before all loops have been performed.
For InxIALast = UBound(InxArray) To 1 Step -1
NoSwap = True
For InxIACrnt = 1 To InxIALast - 1
If ArrFolder.Folders(InxArray(InxIACrnt)).Name > _
ArrFolder.Folders(InxArray(InxIACrnt + 1)).Name Then
NoSwap = False
' Move higher entry one slot towards the end
TempInt = InxArray(InxIACrnt)
InxArray(InxIACrnt) = InxArray(InxIACrnt + 1)
InxArray(InxIACrnt + 1) = TempInt
End If
Next
If NoSwap Then
Exit For
End If
Next
End Sub
您可以在MS Access中鏈接Outlook。 – Fionnuala 2012-04-20 15:00:06
我一直認爲Outlook鏈接表在解決不同工作站,outlook/access版本等方面的兼容性時會遇到更多問題。我是不是認爲正確? – HK1 2012-04-20 15:04:04
我不確定,我沒有用太多,這就是爲什麼我沒有發佈答案。你可以用Outlook和VBA做很多事情,但是我已經完成了一段時間。我通常使用自動化。每封電子郵件都有唯一的ID。 – Fionnuala 2012-04-20 15:07:16