Click here to Skip to main content
15,881,092 members
Articles / VBA
Tip/Trick

Outlook Attachments Counter, Don't Send Mails Without Attachments Anymore

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Feb 2022CPOL2 min read 3.3K   1
When you forget to attach an invoice and your customer doesn't alert you... well, you start thinking about methods to prevent it from happening again.
In this tip, you will find a VBA code snippet that will help you to stop sending mails without the required attachments.

Introduction

In new Microsoft Outlook versions, you can check an option that monitors the usage of certain words and phrases like "Attached" and it warns you when you used those words / phrases and the attachments counter is 0 when you press the Send button.

I guess that this will work well in English and in other main languages out there, but in case you speak something like Catalan... well, it simply does not work.

In order to avoid that situation, I've written a small VBA code snippet that you can freely use and configure to adapt to your needs.

I've noticed images are counted as attachments, therefore you'll have to take that into account to put the limit to your counter. As an example, if I send a mail without attachments from my gmail account (with a simple text signature), the attachments counter returns 0. On the other hand, if I send a mail from my work account (that has one image in the signature), the attachments counter returns 1.

Using the Code

  1. To access the VBA editor in Outlook 365, press ALT+F11.
  2. Select your project, Microsoft Outlook Object and then ThisOutlookSession in the tree at the VBA editor window.
  3. Then copy this code into the editor:
    VBScript
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
      Dim mi As MailItem
      Dim minimumAttachments As Integer
    
      If (TypeName(Item) = "MailItem") Then
        Set mi = Item
    
        If (InStr(1, UCase(mi.SendUsingAccount), UCase("YourWorkDomain.com"))) Then
            minimumAttachments = 1
        Else
            minimumAttachments = 0
        End If
        
        If (InStr(1, UCase(mi.Body), UCase("attach"), vbTextCompare) > 0) Or _
           (InStr(1, UCase(mi.Subject), UCase("attach"), vbTextCompare) > 0) Then
           
          If mi.Attachments.Count <= minimumAttachments Then
            answer = MsgBox("No attachments found, send it anyway?", vbYesNo)
            If answer = vbNo Then Cancel = True
          End If
        End If
      End If
    End Sub

That sub (procedure) is being called when you send an email, then, before the mail is sent, it checks if the text "Attach" appears in the body or the subject of your email and if it appears there, it counts the attachments it has and compares them to the minimum amount there should be to consider there are attachments in your mail.

If there are no attachments, it asks you if you want to send the mail or not. Then in case you decide not to send the mail, the send operation gets aborted and you can make any modification in your email.

Important Detail

In recent Office versions, macros are disabled unless you digitally sign them.

Check "how to use selfcert.exe" in Google, there you'll find information about creating a self certificate to digitally sign your macros.

History

  • 28th February, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer robotecnik
Spain Spain
I'm from Catalonia (in Spain) and a specialist in CNC, PLC, robotics and automation in general.

From 1998 I've been developing software applications for the automation market.

I'm using different technologies in each case to get the proper result, fieldbus communications, special electronics, special laser sensors, artificial vision, robot arms, CNC applications, PLC's...

www.robotecnik.com[^] - robots, CNC and PLC programming

Comments and Discussions

 
GeneralMy vote of 5 Pin
Utku Ozan ÇANKAYA28-Feb-22 23:46
professionalUtku Ozan ÇANKAYA28-Feb-22 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.