Consulting

Results 1 to 7 of 7

Thread: Check for Attachments Before Sending, Excluding Images in Signature

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location

    Check for Attachments Before Sending, Excluding Images in Signature

    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!

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    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.

  3. #3
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location
    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!

    Outlook VBA.jpg

  4. #4
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Perhaps you have to change security settings.

    Enable or disable macros in Office documents

    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.


    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.

  5. #5
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location
    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.

  6. #6
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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-serv...multiple-users and https://www.petri.com/forums/forum/m...a-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
    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.

  7. #7
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location
    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!

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
  •