Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / VBA

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

5.00/5 (2 votes)
27 Feb 2022CPOL2 min read 3.6K  
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)