Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I Hope i'm on the right place :P

i am trying to extract the email address of sender from an outlook email. i got this piece of code and modified a little to allow a user select what folder should the code run into.


VB
Sub GetSMTPaddress()
     
    Dim ns As NameSpace
    Dim fldr As MAPIFolder
    Dim m As MailItem
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlSht As Object
    Dim r As Long
     
     'set up an Excel sheet for the results
    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Add
    Set xlSht = xlWB.Sheets.Add
    xlSht.Name = "SMTPs"
     
     'target the selected folder
    Set ns = Application.GetNamespace("MAPI")
   Set fldr = ns.GetFolderFromID( _
ActiveExplorer.CurrentFolder.EntryID, _
ActiveExplorer.CurrentFolder.StoreID)

     
    r = 1 'used as excel row index
    For Each m In fldr.Items 'loop thru the mail items
        If m.SenderEmailType = "SMTP" Then
             'check it's not already in col A of our results sheet
            If xlSht.Columns(1).Find(m.SenderEmailAddress, LookIn:=xlValues) Is Nothing Then
                xlSht.Cells(r, 1).Value = m.SenderEmailAddress
                r = r + 1 'increment the row index counter
                msg = m.Subject
            End If
        End If
    Next m
     
     'show the results
    xlApp.Visible = True
     
End Sub


i created a dummy folder on my outlook to test the result. i sent an email to my outlook account using another email of mine. used at least 2 email addresses to send test emails to my outlook email.

i selected the folder. then run the script.
well the Excel file should show 2 different email address in rows when i run the code.

Excel doesn't populate the emails from the selected folder. i checked the VBA window of outlook and tested the script if the correct folder is selected by the script and Yes it was able to.

The
VB
Dim m As MailItem
even shows the subject line of the first test mail.



Please let me know where the code went wrong? Thanks in advance.
Posted

1 solution

Hello,

You have to define xlValues

Add at the top of your code:
VB
Const xlValues = -4163


You can find a comprehensive list of Excel constants here:
http://www.datapigtechnologies.com/downloads/Excel_Enumerations.txt[^]



Valery.
 
Share this answer
 
Comments
lgmanuel 31-Jul-14 18:56pm    
Hi Valery,

Thanks for the insight. i will note that link.

I tried your suggestion to add 'Const xlValues = -4163' at the top of the code as you said. i'm sure not what i am doing wrong but when Excel pops out after running the script. No Data is populated. not sure how to troubleshoot as it does not show any error.

tried to place the code anywhere before the code that saves the data to Excel, but no lucks T_T. I hope you can enlighten me more. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900