0

I am working to automate a recurring task.

Search an e-mail inbox (not the primary Outlook account) for items with a subject that contains: "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 ...

....

The above code searches my default e-mail (for instance, jsmith@example.com), however, I have a secondary e-mail called Retail Finance , (rfin@example.com) and I want to search that e-mail addresses inbox. How can I modify my code to accomplish this?

I'm new to Outlook VBA, sorry if this is basic.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
PocketLoan
  • 534
  • 4
  • 13
  • 28

2 Answers2

1

If this is a delegate Exchange mailbox, open it using Namespace.GetSharedDefaultFolder. If it is just another store in your current profile, find the store in the Namespace.Stores collection, and call Store.GetDefaultFolder.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • How do I implement those? When I try: Dim objNamespace As Outlook.NameSpace Set objNamespace = myOlApp.NameSpace.GetSharedDefaultFolder I get: Run-time error '438' Object doesn't support this property or method. – PocketLoan Mar 28 '16 at 17:14
  • 1
    You need myOlApp.GetNameSpace("MAPI").GetSharedDefaultFolder . Pass the Recipient object returned by Namespace.CreateRecipient. – Dmitry Streblechenko Mar 28 '16 at 17:22
0

This code accomplishes the task:

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("rfin@example.com")   '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
PocketLoan
  • 534
  • 4
  • 13
  • 28