Access 2010中的VBA导入位于Outlook公共(子)文件夹中的电子邮件 - 包括文件夹名称&附件?

时间:2013-02-08 03:19:13

标签: vba import access-vba outlook-2010

我正在尝试开发一个Access数据库,用于跟踪Outlook中的电子邮件。通过组合来自许多互联网搜索的点点滴滴,我能够开发出以下代码。附带的代码最终起作用,花了我更多的时间,而不是我想承认开发。我是VBA编程的新手,我正试图在整个过程中苦苦挣扎。无论如何,出于沮丧和恐惧,这个项目最终可能比我想要的更长,我想我最终会寻求一些帮助。以下是按优先顺序排列的功能,我最终希望将其添加到以下代码中:

高优先级:

(1)需要递归VBA代码来导入位于所有子文件夹中的电子邮件。 (2)需要VBA代码将电子邮件所在的文件夹名称插入Access数据库。文件夹路径不是必需的。 (3)需要VBA代码插入任何用户附加文档的文件名。

低优先级(访问可用于删除重复项,直到问题得到解决):

(4)希望VBA代码在运行宏时附加新电子邮件数据。

美好的未来选择:

(5)允许我选择文件夹的VBA代码。选项将允许未来的灵活性。

我在Window 7(64位计算机)上运行Access和Outlook 2010。以下是我目前的代码:

Sub ImportContactsFromOutlook()

   ' This code is based in Microsoft Access.

   ' Set up DAO objects (uses existing "tblContacts" table)
   Dim rst As DAO.Recordset
   Set rst = CurrentDb.OpenRecordset("Email")


   ' Set up Outlook objects.
   Dim ol As New Outlook.Application
   Dim olns As Outlook.NameSpace
   Dim cf As Outlook.MAPIFolder
   Dim c As Outlook.MailItem
   Dim objItems As Outlook.Items
   Dim Prop As Outlook.UserProperty

   Set olns = ol.GetNamespace("MAPI")
   '--- (5) --- VBA code to allow me to pick a folder. Option would allow for future     flexability.
   Set cf = olns.GetDefaultFolder(olPublicFoldersAllPublicFolders)
   '--- (1) --- Need recursive VBA code to import emails located in all subfolders.
   Set objItems = cf.Items
   iNumMessages = objItems.Count
   If iNumMessages <> 0 Then
      For i = 1 To iNumMessages
         If TypeName(objItems(i)) = "MailItem" Then
            Set c = objItems(i)
            rst.AddNew
            rst!EntryID = c.EntryID
            rst!ConversationID = c.ConversationID
            rst!Sender = c.Sender
            rst!SenderName = c.SenderName
            rst!SentOn = c.SentOn
            rst!To = c.To
            rst!CC = c.CC
            rst!BCC = c.BCC
            rst!Subject = c.Subject
            rst!Attachments = c.Attachments.Count
            '--- (3) --- Need VBA code to insert the file name of any user attached     documents. ".Count" is used to avoid error and can be replaced.
            rst!Body = c.Body
            rst!HTMLBody = c.HTMLBody
            rst!Importance = c.Importance
            rst!Size = c.Size
            rst!CreationTime = c.CreationTime
            rst!ReceivedTime = c.ReceivedTime
            rst!ExpiryTime = c.ExpiryTime
            '--- (2) --- Need VBA code to insert the Folder name where the email is     located into Access Database. Folder Path is not necessary.
            rst.Update
         End If
      Next i
      rst.Close
      MsgBox "Finished."
   Else
      MsgBox "No e-mails to export."
   End If
   '--- (4) --- Want VBA code to append data with new emails when macro is run.

End Sub

以下是我尝试使用的一些有用的参考资料。其中一些看起来像花哨的工具。因为我在学习,我要么无法实现,要么无法理解其中的一些......

  • msdn.microsoft.com/en-us/library/ee861519(V = office.14)的.aspx
  • msdn.microsoft.com/en-us/library/office/ee861520(V = office.14)的.aspx
  • accessexperts.net/blog/2011/07/07/importing-outlook-emails-into-access /
  • add-in-express.com/creating-addins-blog/2011/08/15/how-to-get-list-of-attachments /
  • databasejournal.com/features/msaccess/article.php/3827996/Working-With-Outlook-from-Access.htm
  • stackoverflow.com/questions/7298591/copying-all-incoming-emails-in-outlook-inbox-and-personal-subfolders-to-excel-th

欢迎任何建议或指示。谢谢您的帮助。非常感谢。


这是我现在的代码(见下文)。我运行它时仍然存在一些问题。在第一次运行代码时,由于Access数据库表中没有记录,因此收到以下错误:

运行时错误'3021':没有当前记录。

是否有错误检查或我可以编码的方式?此外,在填充Access数据库后,以下代码仅排除在主文件夹中找到的电子邮件,而不是子文件夹:

If ([rst]![EmailLocation] <> ofProp.Name) And ([rst]![EntryID] <> cMail.EntryID) Then

我想找出原因。最后,我仍然需要知道如何将用户附加文档列表拉入访问数据库。以下代码提取所有附件,包括嵌入的附件,并仅返回文档中的第一个附件:

                    Set cAtch = cMail.Attachments
                    cntAtch = cAtch.Count
                    If cntAtch > 0 Then
                        For j = cntAtch To 1 Step -1
                            strAtch = cAtch.Item(j).FileName
                            rst!Attachments = strAtch
                        Next
                    Else
                        rst!Attachments = "No Attachments"
                    End If

再次,任何帮助将不胜感激。感谢。

Sub ImportMailPropFromOutlook()

    ' Code for specifing top level folder and initializing routine.

    ' Set up Outlook objects.
    Dim ol As New Outlook.Application
    Dim olns As Outlook.NameSpace
    Dim ofO As Outlook.MAPIFolder
    Dim ofSubO As Outlook.MAPIFolder
    Dim objItems As Outlook.Items

    Set olns = ol.GetNamespace("MAPI")
    Set ofO = olns.GetDefaultFolder(olFolderInbox) '--- Specifies top level folder for     importing Oultook mail.
    'Set of = olns.PickFolder '--- Allows user to select top level folder for importing     Outlook mail.

    'Set info and call GetMailProp code.
    Set objItems = ofO.Items
    GetMailProp objItems, ofO

    'Set info and call ProcessSubFolders.
    For Each ofSubO In of.Folders
        Set objItems = ofSubO.Items
        ProcessSubFolders objItems, ofSubO
    Next

End Sub

Sub GetMailProp(objProp As Outlook.Items, ofProp As Outlook.MAPIFolder)

    ' Code for writeing Outlook mail properties to Access.

    ' Set up DAO objects (uses existing Access "Email" table).
    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("Email")

    'Set Up Outlook objects.
    Dim cMail As Outlook.MailItem
    Dim cAtch As Outlook.Attachments

    'Write Outlook mail properties to Access "Email" table.
    iNumMessages = objProp.Count
    If iNumMessages <> 0 Then
        For i = 1 To iNumMessages
            If TypeName(objProp(i)) = "MailItem" Then
                Set cMail = objProp(i)
                If ([rst]![EmailLocation] <> ofProp.Name) And ([rst]![EntryID] <>     cMail.EntryID) Then
                    rst.AddNew
                    rst!EntryID = cMail.EntryID
                    rst!ConversationID = cMail.ConversationID
                    rst!Sender = cMail.Sender
                    rst!SenderName = cMail.SenderName
                    rst!SentOn = cMail.SentOn
                    rst!To = cMail.To
                    rst!CC = cMail.CC
                    rst!BCC = cMail.BCC
                    rst!Subject = cMail.Subject
                    Set cAtch = cMail.Attachments
                    cntAtch = cAtch.Count
                    If cntAtch > 0 Then
                        For j = cntAtch To 1 Step -1
                            strAtch = cAtch.Item(j).FileName
                            rst!Attachments = strAtch
                        Next
                    Else
                        rst!Attachments = "No Attachments"
                    End If
                    rst!Count = cMail.Attachments.Count
                    rst!Body = cMail.Body
                    rst!HTMLBody = cMail.HTMLBody
                    rst!Importance = cMail.Importance
                    rst!Size = cMail.Size
                    rst!CreationTime = cMail.CreationTime
                    rst!ReceivedTime = cMail.ReceivedTime
                    rst!ExpiryTime = cMail.ExpiryTime
                    rst!EmailLocation = ofProp.Name
                    rst.Update
                End If
            End If
        Next i
    End If

End Sub

Sub ProcessSubFolders(objItemsR As Outlook.Items, OfR As Outlook.MAPIFolder)

    'Code for processing subfolders

    ' Set up Outlook objects.
    Dim ofSubR As Outlook.MAPIFolder

    'Set info and call GetMailProp code.
    GetMailProp objItemsR, OfR

    'Set info and call ProcessSubFolders. Recursive.
    For Each ofSubR In OfR.Folders
        Set objItemsR = ofSubR.Items
        ProcessSubFolders objItemsR, ofSubR
    Next

End Sub

我有机会更多地处理代码。我想要做的是将位于Outlook帐户的所有子文件夹中的电子邮件导入Access。 VBA代码在Access中。我只需要某些邮件项属性。大多数是您需要在Outlook中复制打印备忘录功能的那些。

我添加了一些我认为需要帮助排除位于同一文件夹中的重复项的内容。这是不同公共子文件夹中的重复电子邮件,但我需要知道在我的数据库记录中。

我仍然需要一个递归子或函数来确保我获得所有子文件夹。我尝试了For / Next循环,但这只搜索一个级别的子文件夹。我可以在这方面挑衅地使用一些帮助。这似乎是一个艰难的部分。

我的更新代码是:

Sub ImportContactsFromOutlook()

   ' This code is based in Microsoft Access.

   ' Set up DAO objects (uses existing "Email" table)
   Dim rst As DAO.Recordset
   Set rst = CurrentDb.OpenRecordset("Email")


   ' Set up Outlook objects.
   Dim ol As New Outlook.Application
   Dim olns As Outlook.NameSpace
   Dim cf As Outlook.MAPIFolder
   Dim cMail As Outlook.MailItem
   Dim cAtch As Outlook.Attachments
   Dim objItems As Outlook.Items
   Dim of As Outlook.Folder
   Dim ofSub As Outlook.Folder

   Set olns = ol.GetNamespace("MAPI")
   '--- (5) ---
   'Would eventually be nice to allow a user to select a folder. Folderpicker? Lowest     priority.

   Set of = olns.GetDefaultFolder(olFolderInbox)
   '--- (1) ---
   'Loop only searches one level down. I will need all subfolders. Most examples I saw     call external Sub? Recursive?
   For Each ofSub In of.Folders
   Set objItems = ofSub.Items
   iNumMessages = objItems.Count
   If iNumMessages <> 0 Then
      For i = 1 To iNumMessages
         If TypeName(objItems(i)) = "MailItem" Then
            Set cMail = objItems(i)
            rst.AddNew
            rst!EntryID = cMail.EntryID
            rst!ConversationID = cMail.ConversationID
            rst!Sender = cMail.Sender
            rst!SenderName = cMail.SenderName
            rst!SentOn = cMail.SentOn
            rst!To = cMail.To
            rst!CC = cMail.CC
            rst!BCC = cMail.BCC
            rst!Subject = cMail.Subject
            '--- (3) ---
            'Code only inserts first attachment. Code Also inserts embedded     attachments.
            'Need code to insert all user selected attachments (ex. PDF Document) and     no embedded attachments.
            Set cAtch = cMail.Attachments
            cntAtch = cAtch.Count
                If cntAtch > 0 Then
                    For j = cntAtch To 1 Step -1
                    strAtch = cAtch.Item(j).FileName
                    rst!Attachments = strAtch
                    Next
                Else
                    rst!Attachments = "No Attachments"
                End If
            rst!Count = cMail.Attachments.Count
            rst!Body = cMail.Body
            rst!HTMLBody = cMail.HTMLBody
            rst!Importance = cMail.Importance
            rst!Size = cMail.Size
            rst!CreationTime = cMail.CreationTime
            rst!ReceivedTime = cMail.ReceivedTime
            rst!ExpiryTime = cMail.ExpiryTime
            '--- (2) ---
            ' Solved - Figured out how to call folder location into databse.
            rst!EmailLocation = ofSub.Name
            rst.Update
         End If
      Next i
   End If
   Next
   '--- (4) ---
   'Still need code to append Access database with only new records.
   'Duplicate email can exist in differenc subfolders but not same subfolder.
End Sub

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我能够在网上找到一些示例来解决排除重复邮件记录和运行时错误'3021',其中包含以下代码:

' If code checks outlook mail for and excludes duplicate records based on table fields [EntryID] and [EmailLocation].
If Cnt = DCount("[EntryID] & [EmailLocation]", "Email", "[EntryID] = """ & cMail.EntryID & """ And [EmailLocation] = """ & ofProp.Name & """") = 0 Then
    'Code used to insert individual outlook mail properties.
End If

仍然需要解决附件问题。任何帮助,将不胜感激。谢谢。

答案 1 :(得分:0)

从Helen Feddema编写的代码中选中此示例以选择Outlook联系人。 “将日历项导出到Excel” http://www.helenfeddema.com/Code%20Samples.htm