-7

I have to send mails to many different groups and sometimes I send to the wrong group.

I want to create VBA code to check, the initial two letters of subject header and also whether the same two initial letters are present in the mail body, as I send the mail. If the escalation is correct then mail should be sent otherwise the code should display some error message.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sandeep R
  • 11
  • 1
  • 2
  • 2
    Stack Overflow is not a code writing service. What have you tried so far? And what *specific* problems are you having? – David Zemens Mar 25 '15 at 16:12
  • If you want to create a macro, show what you tried. Else it looks like you want someone to write that macro for you. – Pankaj Jaju Mar 25 '15 at 16:13
  • You may find http://stackoverflow.com/help/mcve and http://stackoverflow.com/help useful. Edit the question to add details. If it cannot be salvaged then consider deleting this question. As it stands now it can only gather downvotes which impacts your ability to ask questions. http://meta.stackoverflow.com/questions/258757/how-can-i-understand-why-am-i-receiving-a-warning-that-i-could-be-blocked – niton Mar 25 '15 at 18:55

1 Answers1

1

It looks like you need to handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. Be aware, the Cancel parameter passed to the event handler allows to cancel the action. If the event procedure sets this argument to true, the send action is not completed and the inspector is left open.

Public WithEvents myOlApp As Outlook.Application  
Public Sub Initialize_handler()    
   Set myOlApp = Outlook.Application  
End Sub 

Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)  
  Dim prompt As String  
  prompt = "Are you sure you want to send " & Item.Subject & "?"  
  If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then  
    Cancel = True  
  End If  
End Sub

In the code you can check out whatever you need - the Subject, Body or HTMLBody values.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45