Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specified Cast Is Not Valid

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
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
 
Wild guess. Outlook.Items may be 0 based, not 1 based so
olItems.Item(i) has i = count which is 1 greater than the last possibl subscript.

If so, try
For i = boxCount - 1 To 0 Step -1

===============>>>> Exception is kicked off on next line.
email = olItems.Item(i)


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
The exception is being thrown because you're trying to go from an item in Outlook.Items (I guess this collection contains Outlook.Item items, but I'm not sure) to an Outlook.MailItem without any automatic conversion path. Normally, if MailItem inherits from Item, this conversion happens automatically. But in this case the compiler isn't seeing anything it can use for this, so you will have to do it yourself.

Maybe MailItem has a constructor that will accept an object of type Item? Or maybe there's a helper class which can translate for you?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Using Option Strict will uncover most implicit conversion problems at compile time. You will need to explicitly convert values and types using Ctype, DirectCast and System.Convert. I have found it to be well worth it.

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Ok, I got this working. It turns out that if you have an undeliverable mail message, that it the Outlook.MailItem doesn't like it so much, so for this case I resorted to using the Inbox.Items.Item to get what I need. Here is the code that I use and works now.

code block:

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 boxCount As Integer = olItems.Count
Dim i As Integer

Try
For i = boxCount To 1 Step -1
Dim matchValue As String = RegExValue(olItems.Item(i).Subject, "Undeliverable")
Dim invalidEmail As String = RegExValue(olItems.Item(i).body, "\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z0-9._%-]{2,4}\b")
If matchValue.Length > 0 Then
lbInboxPrototype.Items.Add("DELETE: " & olItems.Item(i).Subject)
olItems.Item(i).Delete()
End If
Next i
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top