VBA macro to check Outlook Mail properties

Posted: June 16, 2008 in General Tech

The below macro will ensure that :
1. Subject is not empty
2. You are not missing any attachment

Steps :
1. Open OutLook
2. Press Atl + F11 to open VBA editor
3. Towards left pane, you will find Microsoft Office Outlook objects, expand it.
4. You will find “ThisOutlookSession”
5. Copy paste below macro and save it to project
6. Open Outlook window, Tools tab
7. Click on “TrustCenter” and then “MacroSecurity”
8. Check “No Security Check for Macros” option
9. Close and Open Outlook and this macro will start working

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim strSubject As String strSubject = Item.Subject Dim intRes As IntegerDim strMsg As StringDim strThismsg As StringDim intOldmsgstart As Integer If Len(strSubject) = 0 Then Prompt$ = “Subject is Empty. Are you sure you want to send the Mail?” If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, “Check for Subject”) = vbNo Then Cancel = True End If End If intOldmsgstart = InStr(Item.Body, “—–Original Message—–“)’ intOldmsgstart is the location of where old/re/fwd msg starts. Will be 0 if new msg
If intOldmsgstart = 0 ThenstrThismsg = Item.Body + ” ” + Item.SubjectElsestrThismsg = Left(Item.Body, intOldmsgstart) + ” ” + Item.SubjectEnd If’ The above if/then/else will set strThismsg to be the text of this message only,’ excluding old/fwd/re msg’ IE if the original included message is mentioning an attachment, ignore that’ Also includes the subject line at the end of the strThismsg string
If InStr(LCase(strThismsg), “attachment”) > 0 ThenIf Item.Attachments.Count = 0 ThenstrMsg = “Attachment Checker:” & Chr(13) & Chr(10) & “Your message mentions an attachment, but doesn’t have one.” & Chr(13) & Chr(10) & “Send the message anyway?”intRes = MsgBox(strMsg, vbYesNo + vbDefaultButton2 + vbExclamation, “You forgot the attachment!”)If intRes = vbNo Then’ cancel sendCancel = TrueEnd IfEnd IfEnd If
End Sub

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s