Solved: Parse email body for email addresses and write them to Excel...Help with Troubleshoot
Hi,
I have written a script that parses the bodies of emails for email addresses and writes them to an Excel spreadsheet. I am using a regex to match and an array to store the strings. The script works great except for the three issues below.
BTW, this is being used to get the addresses out of returned mail failures so they can be removed from our database. I only have about 3000+ failures....lol
Issue 1:
I have the all the failure notices in an archive folder to remove them from the server until parsing and then deletion. For the life of me, I cannot reference the archive PST folder. :banghead: It always defaults to the Personal Folders / Inbox / Folder Name Here. The actual path is Archive E-Mails / Inbox / Mail Failures.
Issue 2:
Currently, I have it set to a test folder inside the default inbox labeled "Test". Sometimes it runs fine and sometimes Outlook quits responding and had to be manually shut down. It has never run properly with more than 30 emails in the folder. Even then it's hit and missif it works. Remember I have 3000+ emails that need to be parsed.:dunno
Issue 3:
This should be simple. I would like the output to be to the same Excel file each time, create a new sheet, and plug in the date in A1. Simply for tracking purposes.
I am not asking someone to do the work for me, but please point me in the right direction to fix these issues. My script is realy useless to me without a minimum of issue 2 being corrected. Thank you in advance for everyones help and input.
[VBA]
Sub badAddress()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim regEx As Object
Dim olMatches As Object
Dim strBody As String
Dim bcount As String
Dim lFirstPos As Long
Dim lLastPos As Long
Dim badAddresses As Variant
Dim i As Long
Dim xlApp As Object 'Excel.Application
Dim xlwkbk As Object 'Excel.Workbook
Dim xlwksht As Object 'Excel.Worksheet
Dim xlRng As Object 'Excel.Range
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Test")
Set regEx = CreateObject("VBScript.RegExp")
'define regular expression
regEx.Pattern = "[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}"
regEx.IgnoreCase = True
regEx.Multiline = True
' set up size of variant
bcount = olFolder.Items.Count
ReDim badAddresses(1 To bcount) As String
' initialize variant position counter
i = 0
' parse each message in the folder holding the bounced emails
For Each Item In olFolder.Items
i = i + 1
strBody = Item.Body
Set olMatches = regEx.Execute(strBody)
If olMatches.Count >= 1 Then
badAddresses(i) = olMatches(0)
End If
Next
' write everything to Excel
Set xlApp = GetExcelApp
If xlApp Is Nothing Then GoTo ExitProc
Set xlwkbk = xlApp.Workbooks.Add
Set xlwksht = xlwkbk.Sheets(1)
Set xlRng = xlwksht.Range("A1")
xlApp.ScreenUpdating = False
xlRng.Value = "Bounced email addresses"
' resize version
xlRng.Offset(1, 0).Resize(UBound(badAddresses) + 1).Value = xlApp.Transpose(badAddresses)
xlApp.Visible = True
xlApp.ScreenUpdating = True
ExitProc:
Set xlRng = Nothing
Set xlwksht = Nothing
Set xlwkbk = Nothing
Set xlApp = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
Set badAddresses = Nothing
End Sub
Function GetExcelApp() As Object
' always create new instance
On Error Resume Next
Set GetExcelApp = CreateObject("Excel.Application")
On Error GoTo 0
End Function
[/VBA]
Further Explaination of Index Issue.
Thanks for the reply. The code works just fine for me also.....when certain criteria is met. Yes, the index of the mailitem is referenced using the "i" variable. Search google for "outlook vba mailitem" and the first result will give you more details. I would include a link but I have not posted enough times yet.
[vba]
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.Folders("Archived E-Mail").Folders("Inbox").Folders("Mail Failures")
Set Item = olFolder.Items(2)
Item.Display
[/vba]
The code above should open the second email in the folder by referencing its index. This is where my problem occurs. Index #1 is not always the first email in the folder. The index numbering will still run consecutively but once it reaches the end of the folder it starts back at the top. View examples below.
Ex.A: Fails at index 7 but Starts at index 1
Start of Folder
email 1 - index 7
email 2 - index 8
email 3 - index 9
email 4 - index 10
email 5 - index 1
email 6 - index 2
email 7 - index 3
email 8 - index 4
email 9 - index 5
email 10 - index 6
End of Folder
Ex.B: Works Great
Start of Folder
email 1 - index 1
email 2 - index 2
email 3 - index 3
email 4 - index 4
email 5 - index 5
email 6 - index 6
email 7 - index 7
email 8 - index 8
email 9 - index 9
email 10 - index 10
End of Folder
I added a msgbox at different stages of the loop to test each variable. I also marked all items as unread and added [vba]Item.UnRead = False[/vba] just before it looped to the next item. This showed me where the looped started and failed in the folder. I then used the script above to test each mailitem's index number. This is when I noticed that ex. A always failed and ex. B worked like a champ. To take it one step further I added a msgbox to test the strBody variable. When it came around to index 7, the strBody variable did contain the body string and it froze once it tried to parse to string. I assumed something in the body was conflicting with the regEx, so I moved items with index 1 - 6 to another folder leaving only first four items. Again, I used the script above to see if 7 - 10 became 1 - 4 and they did. Re-ran my primary script and it worked perfectly. It parsed the "problem" email plus all the rest with no issues. I am no expert, but I do not know how the regEx would be causing this issue. Again, I am no expert.
I do know that everytime the mailitem index are like ex. A, it always freezes on the mailitem that returns to the top of the folder. This is the line of code that freezes:
[vba]
Set olMatches = regEx.Execute(strBody)
[/vba]
I am sorry if I am not being clear. I do not know any other way to explain it. I do know that I have done alot of testing and if you can recreate the out of order indexes it will freeze as stated above.
Is there any way to reset the mailitem index to match the order of emails in the folder??? I think that would fix all my problems. I spent many hours searching could not find anything that resembled this problem.
Thank you again for your help,
Joshua
PS - The index counts above are examples only. They do not represent the actual amount of emails I am in need of parsing.