在作業中,我在 Outlook 中有兩個電子郵件帳戶。一個是個人電子郵件,另一個是一般部門電子郵件。
我將如何使用 VBA 獲取 excel 來訪問一般電子郵件并將每封電子郵件的發件人拉入字串?我需要遍歷收件箱中的每封電子郵件,忽略任何子檔案夾中的電子郵件。
這是我到目前為止撰寫的代碼。希望我至少在正確的軌道上。
Public Sub test()
Dim emailApp As Outlook.Application, emailNamespace As Outlook.Namespace
Dim oFolder As MAPIFolder, oMail As Outlook.MailItem
Dim iSelect As Outlook.AccountSelector, iBox As Outlook.Account
Dim tEmailAddress As String
Set emailApp = New Outlook.Application
Set emailNamespace = OutlookApp.GetNamespace("MAPI")
Set oFolder = emailNamespace.GetDefaultFolder(olFolderInbox)
'I think im on the right track here.......
Set iBox = iSelect.SelectedAccount
For Each oMail In oFolder.Items
tEmailAddress = oMail.SenderEmailAddress
'Do other stuff for the project.........
Next
End Sub
編輯:為遇到此問題的下一個人發布完整的代碼示例。
Public Sub test()
Dim emailApplication As Outlook.Application, emailAccounts As Outlook.Accounts
Dim emailAccount As Outlook.Account, tAccount As Outlook.Account
Dim emailStore As Outlook.Store, emailInbox As Outlook.Folder, tMail As Variant
Set emailApplication = New Outlook.Application
Set emailAccounts = emailApplication.Session.Accounts
For Each tAccount In emailAccounts
If tAccount.DisplayName = "[email protected]" Then: Set emailAccount = tAccount
Next
Set emailStore = emailAccount.DeliveryStore
Set emailInbox = emailStore.GetDefaultFolder(olFolderInbox)
On Error Resume Next
For Each tMail In emailInbox.Items
Debug.Print tMail.SenderEmailAddress
Next
Err.Clear
End Sub
uj5u.com熱心網友回復:
以下代碼不是必需的:
'I think im on the right track here.......
Set iBox = iSelect.SelectedAccount
相反,您可能只依賴于GetDefaultFolder允許檢索默認檔案夾的方法(從交付商店):
Set oFolder = emailNamespace.GetDefaultFolder(olFolderInbox)
如果您需要在組態檔中選擇特定商店,您可以使用Namespace.Accounts屬性找到所需的帳戶,該屬性回傳代表當前組態檔中Accounts所有物件的集合物件。Account.DeliveryStore屬性回傳一個物件,該物件表示該帳戶的默認交付商店Account。Store.GetDefaultFolder方法回傳一個物件,該物件表示存盤中的默認檔案夾,并且是引數指定的型別。該方法類似于物件的方法。不同之處在于,此方法獲取與帳戶關聯的交付存盤上的默認檔案夾,而StoreFolderFolderTypeGetDefaultFolderNameSpaceNameSpace.GetDefaultFolder回傳當前組態檔的默認存盤中的默認檔案夾。
我需要遍歷收件箱中的每封電子郵件,忽略任何子檔案夾中的電子郵件。
當前檔案夾僅在您處理Folder.Items集合時才被處理。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/533147.html
