2012-04-20 82 views
0

我需要建立一個工具,允許用戶從他的Outlook中選擇一個電子郵件,所以我可以說電子郵件,然後保存爲.msg文件或交替只是保存附件作爲文件。查找並從MS選擇一個Outlook電子郵件訪問

我絆倒了可能是最簡單的一點點,並允許電子郵件的搜索/過濾的最佳途徑。我需要爲用戶提供一個至少與Outlook略有相似的視圖(例如,文件夾應該是相同的訂單/層次結構。

Outlook對象模型是否具有某種Explorer/Picker/Selection對話框我可以調用用戶選擇電子郵件後,將返回STOREID和的EntryID?或者我需要推出自己的?

我要指出,我已經知道如何保存電子郵件或附件,所以我的問題是,只有處理選擇和電子郵件過濾。

通知你,我在MS Access 2007與Outlook 2007編程這個目標機器可以訪問和Outlook或者2007或2010的版本。

+0

您可以在MS Access中鏈接Outlook。 – Fionnuala 2012-04-20 15:00:06

+0

我一直認爲Outlook鏈接表在解決不同工作站,outlook/access版本等方面的兼容性時會遇到更多問題。我是不是認爲正確? – HK1 2012-04-20 15:04:04

+0

我不確定,我沒有用太多,這就是爲什麼我沒有發佈答案。你可以用Outlook和VBA做很多事情,但是我已經完成了一段時間。我通常使用自動化。每封電子郵件都有唯一的ID。 – Fionnuala 2012-04-20 15:07:16

回答

0

鏈接到Outlook表是好的。問題是Outlook不會爲每封郵件提供唯一的ID,並且如果郵件從一個文件夾移動到另一個文件夾,其ID將更改。顯然不是由理解數據庫的人設計的。

一個更好的辦法可能是創建在Outlook中運行一個Outlook插件,然後執行需要將信息發送給訪問的任務。

0

我很少使用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