Extracting recipient emails

jonny10

New Member
Joined
Jul 25, 2019
Messages
1
I have a code that running on INBOX and extracting , need to modify it then it will be running on all subfolders. Please..
Code:
Private Sub ExtractEmails()
    Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
    Dim olFolder As Outlook.MAPIFolder
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    Dim Item As Object
    Dim objFileSystem As Object
    Dim strTextFile As String
    Dim objTextFile As Object
       
    'Create a new Text file
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTextFile = "C:\test\contacts.txt"
    Set objTextFile = objFileSystem.CreateTextFile(strTextFile, True)
 
    For Each Item In olFolder.Items
        If TypeOf Item Is Outlook.MailItem Then
            Dim oMail As Outlook.MailItem: Set oMail = Item
            'Input the list of extracted email addresses into this Text file
            If InStr(1, oMail.SenderEmailAddress, "@", 0) <> 0 And InStr(1, oMail.SenderEmailAddress, "microsoft", 0) = 0 Then
'                objTextFile.WriteLine oMail.SenderName & "|" & oMail.SenderEmailAddress
                Debug.Print oMail.SenderName & "|" & oMail.SenderEmailAddress
            End If
'          objTextFile.Write (oMail.SenderEmailAddress)
        End If
    Next


    objTextFile.Close
 
    MsgBox "Completed!", vbInformation, "Extract Email Addresses"
End Sub

Private Sub ExtractEmails()
Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
Dim olFolder As Outlook.MAPIFolder
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Dim Item As Object
Dim objFileSystem As Object
Dim strTextFile As String
Dim objTextFile As Object

'Create a new Text file
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTextFile = "C:\test\contacts.txt"
Set objTextFile = objFileSystem.CreateTextFile(strTextFile, True)

For Each Item In olFolder.Items
If TypeOf Item Is Outlook.MailItem Then
Dim oMail As Outlook.MailItem: Set oMail = Item
'Input the list of extracted email addresses into this Text file
If InStr(1, oMail.SenderEmailAddress, "@", 0) <> 0 And InStr(1, oMail.SenderEmailAddress, "microsoft", 0) = 0 Then
' objTextFile.WriteLine oMail.SenderName & "|" & oMail.SenderEmailAddress
Debug.Print oMail.SenderName & "|" & oMail.SenderEmailAddress
End If
' objTextFile.Write (oMail.SenderEmailAddress)
End If
Next


objTextFile.Close

MsgBox "Completed!", vbInformation, "Extract Email Addresses"
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top