excel - 从 Outlook 中的特定文件夹导入电子邮件

标签 excel vba outlook

我目前在 Excel 中使用以下代码来访问无人值守的 Outlook 邮箱中的文件夹,而不是我自己的邮箱。

但是,有没有一种方法可以在代码中设置文件夹,而不是使用文件夹选择器。

Sub Launch_Pad()   
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.PickFolder

n = 2
Cells.ClearContents

Call ProcessFolder(olFolder)

Set olNS = Nothing
Set olFolder = Nothing
Set olApp = Nothing
Set olNS = Nothing
End Sub

Sub ProcessFolder(olfdStart As Outlook.MAPIFolder)

Dim olFolder As Outlook.MAPIFolder
Dim olObject As Object
Dim olMail As Outlook.MailItem
   n = 1
For Each olObject In olfdStart.Items
    If TypeName(olObject) = "MailItem" Then

        n = n + 1
        Set olMail = olObject
            Cells(n, 1) = olMail.Subject
            Cells(n, 2) = olMail.ReceivedTime
            Cells(n, 3) = olMail.Body

    End If
Next
Set olMail = Nothing
Set olFolder = Nothing
Set olObject = Nothing
End Sub

最佳答案

谢谢埃尔德姆

Sub ShareMail()

    Dim olNamespace As Outlook.Namespace
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olRecip As Outlook.Recipient

    Set olApp = Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set olRecip = olNs.CreateRecipient("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0bdb1b9bc90bdb1b9bcfeb3bfbd" rel="noreferrer noopener nofollow">[email protected]</a>")
    Set olFolder = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox).Folders("myfolder")

   Call ProcessFolder(olFolder)

    Set olApp = Nothing
    Set olNs = Nothing
    Set olRecip = Nothing
    Set olFolder = Nothing


End Sub

关于excel - 从 Outlook 中的特定文件夹导入电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43273441/

相关文章:

html - 使用 Perl XML::XSLT 生成 OUTLOOK 有问题的 HTML

excel - 文件对话框错误: type mismatch

excel - 我可以在 VBA 中定义一个常量以在 Excel 工作表中使用吗?

excel - 可能的语法错误? "Expecting ="在来自用户窗体的子调用中

VBA - 显示子程序运行所需的时间

javascript - 从客户端将附件添加到默认电子邮件客户端

excel - VBA找不到宏怎么解决

excel - 运行时错误 1004 : Clearing cell next to dropdown list

Excel WorkbookBeforeClose 事件多次触发

excel - 使用工作表列中的地址发送电子邮件