none
Count instances of specific string in Email RRS feed

  • Question

  • I've looked for similar questions regarding this with no luck. I currently have working code that looks for a specific instance of string in the email body and subject. Upon finding this string, it has userform that takes it in another direction. My intentions are to have it run through the entire email and count how many times it finds this iteration and give that a callable variable for that userform(popup). Here is my code. It comes back with an error that says "InvalidCastException" so i'm guessing this is a conversion error. Any ideas? Thanks!

    
    
    Public Sub Application_ItemSend(ByVal Item As Object, _
    ByRef Cancel As Boolean) Handles Application.ItemSend
    Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem)
    If mailItem IsNot Nothing Then
    Dim attachments = mailItem.Attachments
    For Each attachment As Outlook.Attachment In attachments
    
    AttachmentQuery(attachment, mailItem, Cancel)
    
    Next attachment
    End If
    Dim frm1 As New Form2()
    
    'ERROR HAPPENS HERE
    Dim myString As String = (mailItem.Body) And (mailItem.Subject)
    Dim myLookupChar As String = (Item.Body Like "*#########*") Or (Item.Body Like "*###?##?####*") Or (Item.Subject Like "*#########*") Or (Item.Subject Like "*###?##?####*")
    Dim myCount As Integer = 0
    myCount = ReturnCharCount(myString, myLookupChar)
    
    ' Function Called Here
    MsgBox("Count is " & myCount, MsgBoxStyle.OkOnly)
     
    
    If (Item.Body Like "*#########*") Or (Item.Body Like "*###?##?####*") Or (Item.Subject Like "*#########*") Or (Item.Subject Like "*###?##?####*") Then
    Cancel =
    
    True
    



    Wednesday, December 18, 2013 8:01 PM

All replies

  • Your If block with the test for mailIem IsNot Nothing ends before you reference mailItem on the error line. Move the End closing that If block to the end of the code that references mailIem.

    Ken Slovak MVP - Outlook

    Wednesday, December 18, 2013 8:57 PM
    Moderator
  • Hello letsrollusafa11,

    Note, the mail item could be nothing when you get the Body or Subject properties. Please try to use the following code instead:

    Public Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
            Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem)
            If mailItem IsNot Nothing Then
                Dim attachments = mailItem.Attachments
    
                For Each attachment As Outlook.Attachment In attachments
                    AttachmentQuery(attachment, mailItem, Cancel)
                Next attachment
    
                Dim frm1 As New Form2()
    
                'ERROR HAPPENED HERE because mail item could be NOTHING here
                Dim myString As String = (mailItem.Body) And (mailItem.Subject)
                Dim myLookupChar As String = (Item.Body Like "*#########*") Or (Item.Body Like "*###?##?####*") Or (Item.Subject Like "*#########*") Or (Item.Subject Like "*###?##?####*")
                Dim myCount As Integer = 0
    
                myCount = ReturnCharCount(myString, myLookupChar)
    
                ' Function Called Here
                MsgBox("Count is " & myCount, MsgBoxStyle.OkOnly)
    
    
                If (Item.Body Like "*#########*") Or (Item.Body Like "*###?##?####*") Or (Item.Subject Like "*#########*") Or (Item.Subject Like "*###?##?####*") Then
                    Cancel = True
                End If
            End If
        End Sub

    Thursday, December 19, 2013 12:43 PM
  • I'm getting an item not delcared with "ReturnCharCount". What should this be declared as and where? Thank you so much for your help.
    Thursday, December 19, 2013 11:06 PM
  • Getting the following error. I'm guessing it can't transfer from the body format to a string.

    Thursday, December 19, 2013 11:19 PM
  • Hello letsrollusafa11,

    It looks like you need to re-attach an image.

    > I'm getting an item not delcared with "ReturnCharCount". What should this be declared as and where?

    Where did you get the ReturnCharCount function?


    Friday, December 27, 2013 5:54 PM