excel 从单元格引用Outlook文件夹,value

jchrr9hc  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(148)

我有一个宏来提取邮箱中的所有电子邮件到Excel工作表。
我需要添加一行来指定邮箱。此配置在用户级别保持固定,因为只能在编程级别更改邮箱。
用户在其Outlook中有多个共享邮箱。他们应该选择要提取的邮箱。

Sub ExtraerCorreosDeOutlook()

Dim OutlookApp As Object
Dim ONameSpace As Object
Dim MyFolder As Object
Dim OItem As Object
Dim Fila As Integer
Dim Mailbox As String

Set OutlookApp = CreateObject("Outlook.Application")
Set ONameSpace = OutlookApp.GetNamespace("MAPI")
Mailbox = Sheets(1).Range("A1").Value 'In this cell I select the mailbox.

Set MyFolder = ONameSpace.Folders(Mailbox).Folders(1)
'Set MyFolder = ONameSpace.Folders("correo@gmail.com").Folders(1) 'If I use this line the macro works well, but the mailbox is fixed.

Range(Range("A2"), ActiveCell.SpecialCells(xlLastCell)).ClearContents

Fila = 2

For Each OItem In MyFolder.Items

    Sheets("Hoja1").Cells(Fila, 1).Value = OItem.SenderEmailAddress
    Sheets("Hoja1").Cells(Fila, 2).Value = OItem.To
    Sheets("Hoja1").Cells(Fila, 3).Value = OItem.Subject
    Sheets("Hoja1").Cells(Fila, 4).Value = OItem.ReceivedTime
    Sheets("Hoja1").Cells(Fila, 5).Value = OItem.Body

    Fila = Fila + 1

Next OItem

Set OutlookApp = Nothing
Set ONameSpace = Nothing
Set MyFolder = Nothing

End Sub

我需要使用单元格值定义邮箱变量。

pbpqsu0x

pbpqsu0x1#

使用Range.Text property (Excel)代替值,也使用Trim functions
有关共享邮箱,请参见Excel中的示例

Option Explicit
Public Sub Example()
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim Inbox As Outlook.MAPIFolder
    Dim Items As Outlook.Items
    Dim i As Long

    Dim Mailbox As String
    Mailbox = Trim(Sheets(1).Range("A1").Text)
    
    '// Ref to Outlook Inbox
    ' Make sure to set Microsoft Outlook Object XX.X in the Tools>Reference
    Set olApp = New Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    
    Dim Recip As Outlook.Recipient
    Set Recip = olNS.CreateRecipient(Mailbox)
        Recip.Resolve
        
    Set Inbox = olNS.GetSharedDefaultFolder(Recip, olFolderInbox)

    Set Items = Inbox.Items

    For i = Items.Count To 1 Step -1
        Debug.Print Items(i) '
'       do something with Items
    Next
End Sub
t3psigkw

t3psigkw2#

代码取决于共享邮箱是否可见以及是否在Outlook配置文件中配置。如果可见,则可以使用NameSpace.Stores属性迭代所有邮箱/存储,该属性返回表示当前配置文件中所有Store对象的Stores集合对象。
配置文件定义一个或多个电子邮件帐户,每个电子邮件帐户都与特定类型的服务器相关联。对于Exchange服务器,存储可以位于服务器上、Exchange公用文件夹中、本地个人文件夹文件(.pst)或脱机文件夹文件(.ost)中。对于POP3、IMAP或HTTP电子邮件服务器,存储是.pst文件。
使用StoresStore对象可枚举当前会话中所有存储上的所有文件夹和搜索文件夹。由于获取存储中的根文件夹或搜索文件夹需要打开存储,而打开存储会对性能产生影响,因此在决定继续执行此操作之前,可以检查Store.IsOpen属性。

Sub EnumerateFoldersInStores() 
 Dim colStores As Outlook.Stores 
 Dim oStore As Outlook.Store 
 Dim oRoot As Outlook.Folder 
 
 On Error Resume Next 
 
 Set colStores = Application.Session.Stores 
 For Each oStore In colStores 
   Set oRoot = oStore.GetRootFolder 
   Debug.Print (oRoot.FolderPath) 
   EnumerateFolders oRoot 
 Next 
End Sub 
 
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder) 
 Dim folders As Outlook.folders 
 Dim Folder As Outlook.Folder 
 Dim foldercount As Integer 
 
 On Error Resume Next 
 
 Set folders = oFolder.folders 
 foldercount = folders.Count 
 'Check if there are any folders below oFolder 
 If foldercount Then 
   For Each Folder In folders 
     Debug.Print (Folder.FolderPath) 
     EnumerateFolders Folder 
   Next 
 End If 
End Sub

如果该帐户在Outlook中不可见(在Exchange中共享),则可以考虑使用NameSpace.GetSharedDefaultFolder方法,该方法返回Folder对象,该对象表示指定用户的指定默认文件夹。此方法用于委派方案中,在该方案中,一个用户将对另一个用户的一个或多个默认文件夹(例如,其共享收件箱文件夹)的访问权限委派给另一个用户。

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev") 
 myRecipient.Resolve 
 
 If myRecipient.Resolved Then 
   Call ShowCalendar(myNamespace, myRecipient) 
 End If 
End Sub 
 
Sub ShowCalendar(myNamespace, myRecipient) 
 Dim CalendarFolder As Outlook.Folder 
 
 Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar) 
 CalendarFolder.Display 
End Sub

也可以考虑NameSpace.OpenSharedFolderNameSpace.OpenSharedItem方法。

2admgd59

2admgd593#

谢谢大家。这是我的错误。正如尤金在评论中所说的参考邮箱是错误的。我在单元格中写错了电子邮件。感谢社区的帮助和支持。

相关问题