0
votes

Im creating an outlook Macro to validate an Email attachment and recipient name before sending the mail.

The recipient name can be easily validated through the ItemSend Function on the Outlook session.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)   
Dim Recipients As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i
Dim prompt As String

Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)

If InStr(LCase(recip), "[email protected]") Then
  prompt$ = "You sending this to this to " & Item.To & ". Are you sure you want to send it?"
   If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
     Cancel = True
   End If
End If

Next i  
End Sub

While this helps with recipients, it does not allow to validate the attachment name before sending the mail. i.e Validate the Mail Draft. The code below helps to check for attachments present on the draft but does not help validate it.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(1, Item.Body, "attach", 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

So i tried to add item.Attachment. Name \ item.attachment.FileName but this works only if i attribute it to a outlook MailItem instead of a normal object.

Is it possible to create code to validate the attachment name for certain criteria ( name should conform to certain naming constraints ). The code has already been created and works as a normal macro and not as a session Macro.

Function Segregate_Function(Attach_Name_Pass1 As String)

Dim FullName As String
Dim Recepients As String

Region_Ext = Right(Attach_Name_Pass1, 7)
region = Left(Region_Ext, 3)
'MsgBox region

If region = "ENG" Then
Recepients = "[email protected];[email protected]"
Call Send_Function(Attach_Name_Pass1, Recepients)
Else
MsgBox " Not an Acceptable Attachment. Mail Could not be Generated "
End If
End Function

I would like the above code to execute when clicking on send to validate an attachment name directly, instead of having a procedural Macro running.

Do advice.

1

1 Answers

0
votes

Try testing within ItemSend.

Something like this:

Option Explicit

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

Dim att As attachment
Dim Attach_Name_Pass1 As String
Dim Region_Ext As String
Dim Region  As String

Cancel = False

If Item.Attachments.count = 0 Then
    If MsgBox("There's no attachment, send anyway?", vbYesNo) = vbNo Then Cancel = True

Else
    Debug.Print Item.To
    If InStr(Item.To, "[email protected]") > 0 Or InStr(Item.To, "[email protected]") > 0 Then

        For Each att In Item.Attachments
            Attach_Name_Pass1 = att.DisplayName
            Region_Ext = Right(Attach_Name_Pass1, 7)
            Region = Left(Region_Ext, 3)
            'MsgBox region
            Debug.Print Region

            If Region <> "ENG" Then
                Cancel = True
                MsgBox " Not an Acceptable Attachment. Send cancelled."
                Exit For
            End If
        Next
    End If

End If

End Sub