gpt4 book ai didi

excel - 在 Excel 中使用 VBA 引用收件箱以外的 Outlook 邮箱

转载 作者:行者123 更新时间:2023-12-04 19:49:42 26 4
gpt4 key购买 nike

编辑:我真的想通了!我换了行

Set outlookInbox = outlookApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

Dim NS As Outlook.Namespace
Dim objOwner As Outlook.Recipient

Set NS = outlookApp.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("sharedmailbox@companyname.com")
Objowner.Resolve

If objOwner.Resolved Then
MsgBox objOwner.Name 'You can comment this out if you want

Set outlookInbox = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If

原帖:我有这段在 Excel VBA 中运行的代码,它在我的 Outlook 默认收件箱中搜索特定的发件人和附件名称。然后它将附件保存到我桌面上的指定文件夹,同时使用收到电子邮件的日期重命名文件。

但是,我想编辑代码,使其不在我的默认收件箱中搜索,而是在我的 Outlook 中的另一个共享邮箱中搜索。假设此共享邮箱接收电子邮件的电子邮件地址是 sharedmailbox@companyname.com。这显然与我自己的个人电子邮件地址不同。

如何编辑此代码,使其在这个邮箱中搜索,而不是在我自己的收件箱中搜索?

Option Explicit

Sub GetLatestReport()

'Set a reference to Outlook's object library (Visual Basic >> Tools >> References >> check/select Microsoft Outlook Object Library)

Dim outlookApp As Outlook.Application
Dim outlookInbox As Outlook.MAPIFolder
Dim outlookRestrictItems As Outlook.Items
Dim outlookLatestItem As Outlook.MailItem
Dim outlookAttachment As Outlook.Attachment
Dim attachmentFound As Boolean

Const saveToFolder As String = "C:\Users\jalanger\Desktop\Demo" 'change the save to folder accordingly
Const senderName As String = "Langer, Jaclyn" 'change the sender name accordingly
Const attachmentName As String = "Report on ACBS LC for AMLS (Chandran Panicker)" 'change the attachment name accordingly

Dim SavePath As String


'Create an instance of Outlook
Set outlookApp = New Outlook.Application

'Get the inbox from Outlook
Set outlookInbox = outlookApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

'Filter the items from the inbox based on the sender
Set outlookRestrictItems = outlookInbox.Items.Restrict("[SenderName] = '" & senderName & "'")

'Check whether any items were found
If outlookRestrictItems.Count = 0 Then
MsgBox "No items were found from " & senderName & "!", vbExclamation
Exit Sub
End If

'Sort the filtered items by received time and in descending order
outlookRestrictItems.Sort Property:="[ReceivedTime]", Descending:=True

'Get the latest item from the filtered and sorted items
Set outlookLatestItem = outlookRestrictItems(1)

'Make sure that file extension at the end of this line is correct
SavePath = saveToFolder & "\" & attachmentName & " " & CStr(Format(outlookLatestItem.ReceivedTime, "Long Date")) & ".xls"
MsgBox SavePath

'Loop through each attachment from the latest item until specified file is found
attachmentFound = False
For Each outlookAttachment In outlookLatestItem.Attachments
If Left(UCase(outlookAttachment.FileName), Len(attachmentName)) = UCase(attachmentName) Then
outlookAttachment.SaveAsFile SavePath 'saveToFolder & "\" & outlookAttachment.DisplayName
attachmentFound = True
Exit For
End If
Next outlookAttachment

If attachmentFound Then
MsgBox "The attachment was found and saved to '" & saveToFolder & "'!", vbInformation
Else
MsgBox "No attachment was found!", vbExclamation
End If

Workbooks.Open FileName:=SavePath

End Sub

最佳答案

您可以使用帐户的 DeliveryStore 属性来获取其收件箱。例如:

Sub ResolveName()
Dim ns As NameSpace
Set ns = Application.Session
Dim acc As Account
Dim f As Folder

For Each acc In ns.accounts
MsgBox acc.UserName
If acc = "text@outlook.com" Then
Set f = acc.DeliveryStore.GetDefaultFolder(olFolderInbox)
MsgBox f.Items.count

End If
Next
End Sub

您可以使用 acc = "text@outlook.com"或 acc.UserName 属性进行过滤。

关于excel - 在 Excel 中使用 VBA 引用收件箱以外的 Outlook 邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52785623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com