Consulting

Results 1 to 5 of 5

Thread: Code to stop macro from executing when encountering a specific word

  1. #1
    VBAX Newbie
    Joined
    Oct 2017
    Posts
    2
    Location

    Code to stop macro from executing when encountering a specific word

    Hi, all. After figuring out how to create a VBA code to stop sending e-mails with missing attachments, I am now trying to improve my code.
    I am actually trying to solve two things:
    1) To stop the macro from executing if I am replying to an earlier e-mail that includes buzzwords such as "attachment", "enclosed" etc. I have seen some suggestions with a search function (e.g. searching for the word "From:", which would be the first word to appear in a reply message) , but I just cannot seem to get it right for my code.
    2) To stop the VBA from executing if there is a signature with images (which will be counted as attachments by Outlook). I have also seen some suggestions on how to solve this, but the result is the same - it just doesn't work properly
    .

    Below is my code without indention:

    -----

    Function SearchForAttachWords(ByVal s As String) As Boolean
    Dim v As Variant
    
    For Each v In Array("vedlegg", "vedlagt", "lagt ved", "enclosed", "attached", "Vedlegg", "Vedlagt", "Lagt ved", "Enclosed", "Attached")
    
    If InStr(1, s, v, vbTextCompare) <> 0 Then
    SearchForAttachWords = True
    Exit Function
    End If
    Next
    End Function

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    
    If Item.Attachments.Count > 6 Then Exit Sub
    If Not SearchForAttachWords(Item.Subject & ":" & Item.Body)     Then Exit Sub
    
    Prompt$ = "Du har glemt å legge ved dokumenter. Vil du fremdeles sende mailen?"
    If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Glemt vedlegg?") = vbNo Then
    Cancel = True
    End If
    End Sub

    -----

    Do you guys have any clue what to do next?
    Last edited by SamT; 11-01-2017 at 04:14 PM. Reason: Added Code Formatting Tags with # Icon

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The Function looks perfect.

    As to the Sub, I like this way.
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    
       With Item
        If .Attachments.Count > 6 Then 
           GoTo OutaHere
        ElseIf Not SearchForAttachWords(.Subject & .Body) Then 
          GoTo OutaHere 
        'ElseIf next Condition Then 
        '  GoTo OutaHere 
    End If
    End With
    
        Prompt$ = "Du har glemt å legge ved dokumenter. Vil du fremdeles sende mailen?" 
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Glemt vedlegg?") _
    = vbNo Then Cancel = True 
    
    Exit Sub
    OutaHere:
            Cancel = True 
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Oct 2017
    Posts
    2
    Location
    Quote Originally Posted by SamT View Post
    The Function looks perfect.

    As to the Sub, I like this way.
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    
       With Item
        If .Attachments.Count > 6 Then 
           GoTo OutaHere
        ElseIf Not SearchForAttachWords(.Subject & .Body) Then 
          GoTo OutaHere 
        'ElseIf next Condition Then 
        '  GoTo OutaHere 
    End If
    End With
    
        Prompt$ = "Du har glemt å legge ved dokumenter. Vil du fremdeles sende mailen?" 
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Glemt vedlegg?") _
    = vbNo Then Cancel = True 
    
    Exit Sub
    OutaHere:
            Cancel = True 
    End Sub
    Thank you for indenting my code properly. It definitely looks a bit easier to read now.

    My problem is still that I really don't know how to proceed. If the words "attach" or "enclosed" appear in a previous mail or a previous reply, the code still executes regardless if I don't mention the aforementioned words in my new reply. What I would like to understand is how to make the SearchForAttachWords skip previous messages and only look at the new reply (or new e-mail, for that matter).

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Remove "Not" from here

    ElseIf Not SearchForAttachWords
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    You can use Instr to determine whether "From:" exists. This may not be 100% the start of old mail.

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        
        Dim strPrompt As String
        Dim beforeFromPos As Long
        Dim beforeFrom As String
        If Item.Attachments.count > 6 Then Exit Sub
        
        beforeFromPos = InStr(Item.body, "From:")
        
        If beforeFromPos > 0 Then
            beforeFrom = Left(Item.body, beforeFromPos - 1)
        Else
            beforeFrom = Item.body
        End If
        
        Debug.Print beforeFrom
        
        If Not SearchForAttachWords(Item.Subject & beforeFrom) Then Exit Sub
         
        strPrompt = "Du har glemt å legge ved dokumenter. Vil du fremdeles sende mailen?"
        If MsgBox(strPrompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Glemt vedlegg?") = vbNo Then
            Cancel = True
        End If
        
    End Sub
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •