PDA

View Full Version : Code to stop macro from executing when encountering a specific word



kvitlauk
10-31-2017, 01:24 PM
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?

SamT
11-01-2017, 04:39 PM
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

kvitlauk
11-02-2017, 03:17 PM
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).

SamT
11-03-2017, 08:36 AM
Remove "Not" from here


ElseIf Not SearchForAttachWords

skatonni
11-15-2017, 01:14 PM
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