Log in

View Full Version : Check for Attachments Before Sending, Excluding Images in Signature



Eric89
01-28-2016, 12:21 PM
As mentioned in the title of the thread, I'm trying to get Outlook to check for attachments before sending an email. At this point, I've sent enough emails without the intended attachments that it's becoming increasingly annoying and embarrassing. My goal is that if the body contains "attach" in any way (e.g. attach, attached, attachment), it will check to see if there is indeed an attachment before sending. However, I do have a few images in my signature for my company logo, and a few icons for social media links. Thus, the code needs to detect whether there is an attachment exceeding a certain size, and if there is no attachment exceeding that size, it prompts me with a message saying that the email contains no attachment.

I found a VBA code online that seemed to match what I want, but it's not working at all. I tried editing the code to get it to work the way I want, but nothing I seem to do is working. The largest size of the image I have in my signature is about 18kb, but even if I try telling it to prompt me if there is no attachment larger than 18mb, the message still sends. This is the code I have so far:

-----

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim FoundAtt As Boolean

If InStr(1, Item.Body, "attached", vbTextCompare) > 0 Then
If Item.Attachments.Count = 0 Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If

If Item.Attachments.Count > 0 Then


FoundAtt = False

For Each oAtt In Item.Attachments
Debug.Print oAtt.Size
If oAtt.Size < 18200000 Then
GoTo NextAtt

Else
FoundAtt = True
If FoundAtt = True Then Exit Sub

End If
NextAtt:
Next oAtt

If FoundAtt = False Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then Cancel = True
End If
End If
End If
End Sub

-----

Could anybody please let me know what changes I need to make to get this to work? I'm running Outlook 2007. Thanks!

skatonni
01-28-2016, 01:40 PM
Try this:


Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, cancel As Boolean)
Dim FoundAtt As Boolean
Dim answer As VbMsgBoxResult
Dim oAtt As attachment
If InStr(1, Item.Body, "attached", vbTextCompare) > 0 Then
If Item.Attachments.count <= 0 Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then cancel = True
Exit Sub

Else
FoundAtt = False

For Each oAtt In Item.Attachments
Debug.Print oAtt.Size
If oAtt.Size > 18200000 Then
FoundAtt = True
Exit For
End If
Next oAtt
If FoundAtt = False Then
answer = MsgBox("There's no attachment, send anyway?", vbYesNo)
If answer = vbNo Then cancel = True
End If
End If
End If

End Sub

Eric89
01-28-2016, 01:59 PM
Thanks for the quick reply Mark! Unfortunately, I'm still not having any luck. I just tried changing the signature to one that only has a single icon for LinkedIn (about 14 x 14 px), and even tried removing the signature altogether, so it only had "attached" in the body. I have this code entered in "ThisOutlookSession", but I'm not sure what else I might need to do to make this work. You have any ideas what might be wrong? I've attached an image below if it helps... Thanks again!

15311

skatonni
01-28-2016, 08:14 PM
Perhaps you have to change security settings.

Enable or disable macros in Office documents (https://support.office.com/en-us/article/Enable-or-disable-macros-in-Office-documents-7b4fdd2e-174f-47e2-9611-9efe4f860b12)

On the Tools menu, click Trust Center.

Click Macro Settings.

Click the options that you want:

No warnings and disable all macros Click this option if you don't trust macros. All macros and security alerts about macros are disabled.

Warnings for signed macros; all unsigned macros are disabled This is the default setting and is the same as the Disable all macros with notification option, except that if the macro is digitally signed by a trusted publisher, the macro can run if you have already trusted the publisher. If you have not trusted the publisher, you are notified. That way, you can choose to enable those signed macros or trust the publisher. All unsigned macros are disabled without notification.

Warnings for all macros Click this option if you want macros to be disabled, but you want to get security alerts if there are macros present. This way, you can choose when to enable those macros on a case by case basis.

No security check for macros (Not recommended) Click this option to allow all macros to run. This setting makes your computer vulnerable to potentially malicious code and is not recommended.


(https://support.office.com/en-us/article/Enable-or-disable-macros-in-Office-documents-7b4fdd2e-174f-47e2-9611-9efe4f860b12)

Eric89
01-29-2016, 07:53 AM
Thanks skatonni! I found that I was set for option 2 with the signed macros, so I tried options 3 & 4 but still with no luck. Do I need to actually assign this as a macro somehow? I've only followed the brief directions I found on the website with the original code, which just said to start the VBA project in ThisOutlookSession. If I go to Tools > Macro > Macros (Alt + F8), it's empty.

skatonni
01-29-2016, 06:28 PM
If you restarted and still the code did not work, perhaps there is a Group Policy to lock down macros. For reference http://windowsitpro.com/windows-server/access-denied-configuring-office-macro-security-multiple-users and https://www.petri.com/forums/forum/microsoft-networking-services/gpo/23107-change-macro-security-for-ms-outlook-via-group-policy

You cannot see code where you pass parameters, in the macro dialog. You can see standalone code listed.

Open any mailitem or create one as though you were sending, then run this.


Option Explicit

Sub test_Application_ItemSend
Dim cancel as Boolean
Cancel = False
MsgBox "Not locked down?"
Application_ItemSend activeinspector.currentitem, cancel
End Sub

Eric89
02-01-2016, 09:32 AM
I ended up calling our IT guy, and he did indeed have a lock down on macros. I explained what I was trying to do and he took a look at the code, and created an exception since everything seemed fine. Your previous code is now working like a charm! Thanks again!