0

I want to find mail in the olFolderSentMail folder, using the domains of the email addresses, with VBA.

When I make an advanced filter in Outlook, the result comes. I got the sql code from there. There is no result in VBA.

For example, I want to find a@kpmg.com, b@kpmg.com both together. Only names appear in the whom section in Outlook as a and b. Mail addresses are hidden in names.

Sub sentmails()
 
Dim objNS As Outlook.Namespace: Set objNS = GetNamespace("MAPI") Dim
olFolder As Outlook.MAPIFolder

Set olFolder = objNS.GetDefaultFolder(olFolderSentMail)

filterstr = "@SQL=(""http://schemas.microsoft.com/mapi/proptag/0x0e04001f""
CI_STARTSWITH 'kpmg' OR ""http://schemas.microsoft.com/mapi/proptag/0x0e03001f"" CI_STARTSWITH 'kpmg')"

Set arama = olFolder.Items.Restrict(filterstr)

End Sub
Community
  • 1
  • 1
  • The question cannot be easily understood which is possibly why there was a downvote. It appears to be about the known issue of SQL success vs VBA fail. See https://stackoverflow.com/questions/55924883/how-to-filter-an-outlook-view-in-vba-based-on-to-email-addresses and https://stackoverflow.com/questions/63413285/search-by-email-address-for-latest-email-in-all-folders-and-reply-all – niton Nov 11 '20 at 18:00
  • When I search outlook, the result comes out, I use the sql query in outlook in vba, the result is not coming. – erdem akdemir Nov 11 '20 at 21:18
  • Appears I understood the question. The links appear to agree, in this case, the incorrect result is expected with VBA. – niton Nov 11 '20 at 21:31

1 Answers1

0

Firstly, you are not searching for a suffix (*kpmg.com), you are searching for a prefix (kpmg*). Secondly, To/CC/BCC properties might not even contain email addresses, only names.

On the Extended MAPI level (C++ or Delphi), one can create a subrestriction on the message recipients, but Outlook Object Model does not expose it.

If using Redemption (I am its author) is an option, you can use a script similar to the one below:

Set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
Set folder = Session.GetDefaultFolder(olFolderSentMail)
set restItems = folder.Items.Restrict("Recipients LIKE '%kpmg.com%' ")
MsgBox restItems.Count
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78