E-mail en naam extraheren uit geëxporteerd .CSV-bestand van MailBox.
01 | Sub readCSV() |
02 |
03 | Dim strBestand As String |
04 | Dim strRegel As String |
05 | |
06 | Dim objRegExEmail As Object |
07 | Dim objRegExEmailNaam As Object |
08 | Dim strEmail |
09 | Dim strEmailNaam |
10 | |
11 | ' Via regular expression e-mail en naam extraheren |
12 | Set objRegExEmail = CreateObject( "VBScript.RegExp" ) |
13 | Set objRegExEmailNaam = CreateObject( "VBScript.RegExp" ) |
14 | |
15 | ' Regular expression patronen |
16 | objRegExEmail.Pattern = "[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-zA-Z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" |
17 | objRegExEmailNaam.Pattern = ":\s*(.*?)\s*[[<]" |
18 | |
19 | ' Geexporteerd Outlook .csv-bestand inlezen |
20 | strBestand = ThisWorkbook.Path & Application.PathSeparator & "bron.csv" |
21 | |
22 | Open strBestand For Input As #1 |
23 | Do |
24 | Line Input #1, strRegel |
25 | |
26 | If InStr(1, strRegel, "@" ) And InStr(1, strRegel, "Van: " ) Then |
27 | |
28 | Debug.Print "Regel: " & vbTab & strRegel |
29 | |
30 | If objRegExEmail.Test(strRegel) Then |
31 | ' Standaardwaarde regular expression |
32 | Set strEmail = objRegExEmail.Execute(strRegel) |
33 | Debug.Print "E-mail: " & vbTab & strEmail(0).Value |
34 | End If |
35 | |
36 | If objRegExEmailNaam.Test(strRegel) Then |
37 | ' Groepswaarde (submatch) regular expression |
38 | Set strEmailNaam = objRegExEmailNaam.Execute(strRegel) |
39 | Debug.Print "Naam: " & vbTab & vbTab & strEmailNaam(0).Submatches.Item(0) |
40 | End If |
41 | |
42 | Debug.Print vbNewLine |
43 | |
44 | End If |
45 | |
46 | Loop While Not EOF(1) |
47 |
48 | Close #1 |
49 | |
50 | End Sub |
alternatief: https://www.msoutlook.info/question/869
Twee events voor inlezen e-mail: 1. bij ontvangen (outInboxItems_ItemAdd) en 2. bij selecteren (outExplorer_SelectionChange).
01 | Option Explicit |
02 |
03 | Private WithEvents outInboxItems As Outlook.Items |
04 | Public WithEvents outExplorer As Outlook.Explorer |
05 |
06 | Dim strID As String |
07 |
08 | Private Sub Application_Startup() |
09 |
10 | Dim outNS As Outlook.NameSpace |
11 | |
12 | Set outNS = Application.GetNamespace( "MAPI" ) |
13 |
14 | Set outInboxItems = outNS.GetDefaultFolder(olFolderInbox).Items |
15 | Set outExplorer = Application.ActiveExplorer |
16 |
17 | End Sub |
18 | |
19 | Private Sub outExplorer_SelectionChange() |
20 | |
21 | readMail outExplorer.Selection.item(1) |
22 | |
23 | End Sub |
24 |
25 | Private Sub outInboxItems_ItemAdd( ByVal item As Object ) |
26 | |
27 | readMail item |
28 |
29 | End Sub |
30 |
31 | Sub readMail(item As Object ) |
32 |
33 | ' ######################################## |
34 | ' ter voorkoming van meerdere aanroepingen |
35 | |
36 | If strID = item.EntryID Then Exit Sub |
37 | strID = item.EntryID |
38 | |
39 | ' ######################################## |
40 | |
41 | If TypeName(item) = "MailItem" Then |
42 | MsgBox "Afzender: " & item.SenderEmailAddress & vbCrLf & _ |
43 | "Namens: " & item.SentOn & vbCrLf & _ |
44 | "Ontvangen: " & item.ReceivedTime & vbCrLf & _ |
45 | "Onderwerp: " & item.Subject & vbCrLf & _ |
46 | "Grootte: " & item.Size & vbCrLf & _ |
47 | "Bericht: " & vbCrLf & item.Body |
48 | End If |
49 |
50 | End Sub |