Oct 25, 2012

Macro to missing attachment Outlook-VBA Excel Programming


I just faced problems in sending mails without subject or to wrong recipients(later again recalling it). So I felt like I can write some macro for it. And here it is.
If it’s known to you its ok. Else you can use it.
Steps to be followed :
1. Open outlook
2.  Press Alt + F11 or click Tools –> Macro –> Visual Basic Editor to open the VB editor
3.  Press Ctrl + R or click view –> Project Explorer to open the project on the left side pane.
4.  Expand Project1 and you would find “Microsoft Office Outlook Objects”, Expanding this would show you “ThisOutlookSession”.
5.  Double click on “ThisOutlookSession” and paste the following lines.

‘------------------------------------------------Start of code for missing attachments
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
' Declare a variable to save the body content.
Dim strBody As String

' Conver the body content to lowercase.
strBody = LCase(Item.Body)

' /* Check if the body contains the keyword "attach". */
If Not strBody Like "*attach*" Then
Exit Sub
End If

' /* Check the number of attachment(s). */
If Item.Attachments.Count = 0 Then
MsgBox "No Attachment currently.", vbExclamation, "Message"
Cancel = True
End If
End Sub
‘------------------------------------------------end of code for missing attachments
Save the project and close it. Now try sending an email without subject line. A message box would pop up saying that there is no subject line and would you wish to continue sending the mail. Clicking “yes” would continue sending the mail and Clicking “No” would stop your mail. Once subject is done you will get a recipient check. That’s it.

NOTE:  If you restart outlook or the system.. it  may not won’t work as this macro is not digitally signed. For that Adjust Macro Security or use digital signature.





No comments:

Post a Comment