Outlook – Add missing subject & missing attachment checker

This happens with most of us while sending out emails in a hurry, we miss the subject or the attachments or even worse miss them both. Here’s a macro which will prevent that.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim strSubject As String
    strSubject = Item.Subject
    If Len(strSubject) = 0 Then
        Prompt$ = "Subject is missing. Are you sure you want to send the email? "
        If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Subject Missing") = vbNo Then
                Cancel = True
        End If
    End If
    Dim m As Variant
    Dim strBody As String
    Dim intIn As Long
    Dim intNumberOfAttachments As Integer, intZero As Integer
    
    On Error GoTo handleError
    
    intZero = 0
    
    strBody = LCase(Item.Body)
    
    intIn = InStr(1, strBody, "original message")
    
    If intIn = 0 Then intIn = Len(strBody)
    
    intIn = InStr(1, Left(strBody, intIn), "attach")
    If intIn = 0 Then intIn = InStr(1, Left(strBody, Len(strBody)), "pfa")
    
    intNumberOfAttachments = Item.Attachments.count
    
    If intIn > 0 And intNumberOfAttachments <= intZero Then
    
    m = MsgBox("The words attach/pfa were found in this mail," & vbCrLf & "but there is no attachment in this mail." & vbCrLf & vbCrLf & "Do you still want to send?", vbQuestion + vbYesNo + vbMsgBoxSetForeground)
    
    If m = vbNo Then Cancel = True
    
    End If
    
handleError:
    
    If Err.Number <> 0 Then
    MsgBox "Outlook Attachment Reminder Error: " & Err.Description, vbExclamation, "Attachment Missing"
    End If


End Sub
Advertisements

Configure Outlook to generate email from a template on reminder

If you want to send out email using Outlook in a particular format at a particular time, just make use of templates & reminders and configure the following macro to integrate them.

a) Create an outlook template just like the mail you want to send and save it at a convenient location. To learn how to create an outlook template, read this:

b) Create a new function in VBA editor to generate an email from the template.

  1. Open up the VB editor [Alt+F11 in Office 2007 or Tools → Macro → Visual Basic Editor]
  2. Under ThisOutlookSession add the following function:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    Sub createMailFromTemplate()
      Set oApp = CreateObject("Outlook.Application")
      'Give path of the template that was created.
      templateFile = "D:\data\myTemplate.oft"
      Set oMail = oApp.CreateItemFromTemplate(templateFile)
      'Modify the subject
      oMail.Subject = "Daily data for " & Format(Date, "dd.mm.yyyy")
      oMail.Display
    End Sub

c) Finally, add a function to call createMailFromTemplate() [created above] when the reminder pops-up. To learn how to create an Outlook reminder, read this:

  1. Open up the VB editor.
  2. Under ThisOutlookSession add the following function:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    Private Sub Application_Reminder(ByVal Item As Object)
      Dim strSubject As String
      strSubject = Item.Subject
      Dim count As Long
      ' Check the subject of the reminder.
      count = InStr(1, strSubject, "daily data mail")
      If count > 0 Then
        Call createMailFromTemplate
      End If
    End Sub