1

I would like to explore my Outlook messages and if a message comes from email address xx@xxx, save the message in a local folder.

I tried this:

Sub FindIt()
    Dim myOlApp As New Outlook.Application
    Dim myNameSpace As Outlook.Namespace
    Dim myInbox As Outlook.MAPIFolder
    Dim myitems As Outlook.Items
    Dim myitem As Object
     
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myitems = myInbox.Items
     
    For Each myitem In myitems
        If myitem.SenderEmailAddress = "xx@xx" Then
            MsgBox (myitem.Subject)
        End If
    Next myitem
End Sub

I either get an error or I get nothing.

If this code works I will adjust it by replacing the MsgBox with SaveAs (XXXXX).

Community
  • 1
  • 1
MinatoDBO
  • 45
  • 1
  • 7
  • Possible duplicate of [SenderEmailAddress property does not contain a standard email address for internal contacts](https://stackoverflow.com/questions/36900156/senderemailaddress-property-does-not-contain-a-standard-email-address-for-intern) – niton Dec 08 '20 at 20:44
  • it doesn't work for me :/ – MinatoDBO Dec 08 '20 at 21:20
  • Run code in Outlook not Excel. https://stackoverflow.com/questions/52776954/sender-senderemailaddress-missing-in-folder-items – niton Dec 08 '20 at 21:30
  • Yes I tried in Outlook but nothing happened , the macro wasn't detected – MinatoDBO Dec 08 '20 at 21:57

1 Answers1

0

Here is a condensed version of your code which I tested in Excel 365. Note that I set a reference to the Microsoft Outlook Object Library (Tools > References).

Sub FindIt()

    Dim myNameSpace As Outlook.Namespace
    Dim myInbox As Outlook.MAPIFolder
    Dim myItem As Object
    
    Dim i As Integer
     
    Set myNameSpace = Outlook.GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    MsgBox "There are " & myInbox.Items.Count & " items in my Inbox."
    For Each myItem In myInbox.Items
        i = i + 1
'        If myitem.SenderEmailAddress = "xx@xx" Then
            MsgBox (myItem.Subject) & vbCr & myItem.SenderEmailAddress
'        End If
        If i > 3 Then Exit For
    Next myItem
End Sub

This code isn't substantially different from your own except that it's laid out for testing. I hope it will give you an idea of what to look for next.

Variatus
  • 14,293
  • 2
  • 14
  • 30
  • Hello , first many thanks for your help , it works to get the number of message I got in my mail box but nothing happens for the If statement – MinatoDBO Dec 09 '20 at 07:46
  • The IF statement is remmed out. If there are items in the Inbox you should get a Msg for each item with the sender's email address. You can compare that to the search item in your IF statement and find out why it doesn't catch. Bear in mind that `myitem.SenderEmailAddress = "xx@xx"` is very precise. You may be better off with `Instr(1, myitem.SenderEmailAddress,"xx@xx",vbTextCompare) > 0` which will return a number if "xx@xx" is found anywhere in the string, case insensitive. – Variatus Dec 10 '20 at 01:27
  • thanks for your precision but I don't really understand the problem about the If statement here ? I put a good adress , I have many mails from it in my mailbox but nothing happens when I use my macro I get 0 msgbox whereas I should have 3 msgbox – MinatoDBO Dec 10 '20 at 13:47
  • I replaced in my code the statement with your instruction Instr(...) I get nothing back too – MinatoDBO Dec 10 '20 at 13:51
  • How many messages are in the Inbox? Don't look at the Inbox for this information but at the MsgBox the code displays. For testing, run the code with the IF statement remmed out, just as shown in my answer. – Variatus Dec 10 '20 at 23:53
  • I understood , I had 3000 msg in my Inbox and the MsgBox printed it thanks to the ItemCount but if I added a condition which is "from a specific sender : xx@xx" I get nothing (to test it I added a msg box within the in printing "ok") – MinatoDBO Dec 12 '20 at 11:23
  • The weird thing is that if I replace myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) by myInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Folders("XXXX") and I create a specific folder in Outlook it works – MinatoDBO Dec 12 '20 at 11:28
  • This is probably so because your Inbox has subfolders. In summary, it wasn't the code that gave you a problem but its testing. Be consoled. I spend half my time writing testing procedures. As the above example shows, they often consist of ways to learn about interim data. The *Locals* window helps with that when stepping through code but you need to get away from "why doesn't it work?" and reach "what does the code actually do?" – Variatus Dec 13 '20 at 03:13