I am getting a "specified cast is not valid" exception when I get to the specified code below. Can someone lend any insight on how to correct this? Thanks
Try
Dim olApp As Outlook.Application = New Outlook.Application
Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI"
Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
olNS.Logon(Nothing, Nothing, False, True)
Dim olItems As Outlook.Items = olInbox.Items
Dim email As Outlook.MailItem
Dim boxCount As Integer = olItems.Count
Dim i As Integer
Try
For i = boxCount To 1 Step -1
===============>>>> Exception is kicked off on next line.
email = olItems.Item(i)
Dim matchValue As String = RegExValue(email.Subject, "Undeliverable"
Dim invalidEmail As String = RegExValue(email.body, "\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z0-9._%-]{2,4}\b"
If matchValue.Length = 0 Then
email.Delete()
Else
lbInboxPrototype.Items.Add(email.Subject & " >>> " & invalidEmail)
End If
Next
olNS.Logoff()
Catch ex As Exception
txtException.Text = ex.ToString
txtException.Visible = True
Finally
olApp = Nothing
olNS = Nothing
olItems = Nothing
olInbox = Nothing
End Try
Catch outEx As Exception
txtException.Text = outEx.ToString
txtException.Visible = True
End Try
regards,
Brian
Try
Dim olApp As Outlook.Application = New Outlook.Application
Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI"
Dim olInbox As Outlook.MAPIFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
olNS.Logon(Nothing, Nothing, False, True)
Dim olItems As Outlook.Items = olInbox.Items
Dim email As Outlook.MailItem
Dim boxCount As Integer = olItems.Count
Dim i As Integer
Try
For i = boxCount To 1 Step -1
===============>>>> Exception is kicked off on next line.
email = olItems.Item(i)
Dim matchValue As String = RegExValue(email.Subject, "Undeliverable"
Dim invalidEmail As String = RegExValue(email.body, "\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z0-9._%-]{2,4}\b"
If matchValue.Length = 0 Then
email.Delete()
Else
lbInboxPrototype.Items.Add(email.Subject & " >>> " & invalidEmail)
End If
Next
olNS.Logoff()
Catch ex As Exception
txtException.Text = ex.ToString
txtException.Visible = True
Finally
olApp = Nothing
olNS = Nothing
olItems = Nothing
olInbox = Nothing
End Try
Catch outEx As Exception
txtException.Text = outEx.ToString
txtException.Visible = True
End Try
regards,
Brian