2016-03-28 77 views
0

我正在努力自動執行重複性任務。搜索次要Outlook帳戶收件箱

搜索主題包含以下內容的項目的電子郵件收件箱(不是主要Outlook帳戶):「Acting/Additional」。

Sub SrchRF4AAABonuses() 
Dim myOlApp As New Outlook.Application 
Dim objNamespace As Outlook.NameSpace 
Dim objFolder As Outlook.MAPIFolder 
Dim filteredItems As Outlook.Items 
Dim itm As Object 
Dim Found As Boolean 
Dim strFilter As String 

Set objNamespace = myOlApp.GetNamespace("MAPI") 
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox) 
strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%Acting/Additional%'" 

Set filteredItems = objFolder.Items.Restrict(strFilter) 

If filteredItems.Count = 0 Then 
Debug.Print "No emails found" 
Found = False 
Else 
Found = True 
' this loop is optional, it displays the list of emails by subject. 
For Each itm In filteredItems 
Debug.Print itm.Subject 
Next 
End If ... 

....

上面的代碼搜索我的默認電子郵件(例如,[email protected]),但是,我有一個輔助電子郵件稱爲零售金融,(RFIN @ example.com),我想搜索那個電子郵件地址收件箱。我如何修改我的代碼來完成此操作?

我是Outlook VBA的新手,對不起,如果這是基本的。

回答

1

如果這是委託Exchange郵箱,請使用Namespace.GetSharedDefaultFolder打開它。如果它只是您當前配置文件中的另一個商店,請在Namespace.Stores集合中找到該商店,並致電Store.GetDefaultFolder

+0

如何實現這些?當我嘗試: 昏暗objNamespace作爲Outlook.NameSpace 設置objNamespace = myOlApp.NameSpace.GetSharedDefaultFolder 我得到: 運行時錯誤「438」對象不支持此屬性或方法。 –

+1

您需要myOlApp.GetNameSpace(「MAPI」)。GetSharedDefaultFolder。傳遞由Namespace.CreateRecipient返回的收件人對象。 –

0

此代碼完成了任務:

Sub Search_Inbox() 

Dim objNamespace As Outlook.NameSpace 
Dim olShareName As Outlook.Recipient 
Dim olShareInbox As Outlook.Folder 
Dim objFolder As Outlook.MAPIFolder 
Dim filteredItems As Outlook.Items 
Dim itm As Object 
Dim Found As Boolean 
Dim strFilter As String 


Set objNamespace = Application.GetNamespace("MAPI") 
Set olShareName = objNamespace.CreateRecipient("[email protected]") 'address 

Set objFolder = objNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox) 


strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%Acting/Additional%'" 

Set filteredItems = objFolder.Items.Restrict(strFilter) 

If filteredItems.Count = 0 Then 
    Debug.Print "No emails found" 
    Found = False