johncassell
06-03-2015, 02:05 AM
Hi, could someone help me figure this out please. I have the following code to check for missing attachments (I am going to make this include other things later on) but the problem is that the MsgBox pops up BEHIND the email (So the user has no idea that they are being prompted, they just think Outlook has hung). Any advice greatly appreciated. John
Private Sub NewApplication_ItemSend(ByVal Item As Object, Cancel As Boolean)
'## CHECK IF ITEM IS NEW E-MAIL BEFORE PROCEEDING:
If (Item.Class <> olMail) Then Exit Sub ' Ensure this is a mail message and not a task or other "item".
If (Item.Recipients.Count = 0) Then Exit Sub ' Exit early if there are no recipients.
'## CHECK FOR MISSING ATTACHMENT:
Dim mailContent As String
Dim pos As Integer
mailContent = Item.Body + Item.Subject ' Get a copy of all the e-mail body text and subject text to search.
mailContent = LCase(mailContent) ' Make whole string lowercase for easier searching.
If (Item.Attachments.Count = 0) Then ' If there are no attachments:
pos = InStr(1, mailContent, "attach")
If (pos > 0) Then ' If the word 'attach' appears:
res = MsgBox("You have used the word 'attach', but there is no attached file." & vbNewLine & _
"Do you wish to ignore this and send anyway?", vbYesNo)
If res = vbNo Then
Cancel = True
Exit Sub
End If
End If
End If
End Sub
Private Sub NewApplication_ItemSend(ByVal Item As Object, Cancel As Boolean)
'## CHECK IF ITEM IS NEW E-MAIL BEFORE PROCEEDING:
If (Item.Class <> olMail) Then Exit Sub ' Ensure this is a mail message and not a task or other "item".
If (Item.Recipients.Count = 0) Then Exit Sub ' Exit early if there are no recipients.
'## CHECK FOR MISSING ATTACHMENT:
Dim mailContent As String
Dim pos As Integer
mailContent = Item.Body + Item.Subject ' Get a copy of all the e-mail body text and subject text to search.
mailContent = LCase(mailContent) ' Make whole string lowercase for easier searching.
If (Item.Attachments.Count = 0) Then ' If there are no attachments:
pos = InStr(1, mailContent, "attach")
If (pos > 0) Then ' If the word 'attach' appears:
res = MsgBox("You have used the word 'attach', but there is no attached file." & vbNewLine & _
"Do you wish to ignore this and send anyway?", vbYesNo)
If res = vbNo Then
Cancel = True
Exit Sub
End If
End If
End If
End Sub