Handy Outlook Attachment Reminder Macro
Have you ever had one of those “oh shit” moments where two seconds after sending an email you realized something was missing? And you know, by using the Outlook “Recall Message” feature, you’re just going to draw even more attention to your mistake. It’s like a highway accident… people will open recalled messages before anything else in their Inbox just to see what you might have screwed up.
Here’s one easy way to prevent that from happening: use this Outlook VBA macro to automatically scan your outgoing messages and stop incomplete emails & invites before they get sent. This macro will check for missing attachments on messages & meeting invites where you probably meant to attach something. It will also check for blank subject lines, as well as blank locations in meeting invites.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Attachment Reminder Macro
'Carl C, 17-Sep-08
'http://manage-this.com
'
'Paste this macro into your "ThisOutlookSession" module.
'DISCLAIMER: This was only tested on Outlook 2003
'
'ADDITIONAL CREDITS:
'This macro was created by modifying Mark Bird's original code which
'can be found here: http://mark.bird.googlepages.com/, fixing some
'issues related to search strings on replies/forwards, and also
'merging in additional features/checks taken from examples on Sue
'Mosher's OutlookCode.com. See examples here:
'http://www.outlookcode.com/codedetail.aspx?id=539 and here:
'http://www.outlookcode.com/codedetail.aspx?id=1278 Thanks to Mark
'Bird, Jeremy Gollehon, Steve Bateman, and of course, Sue Mosher
'
Dim m As Variant
Dim strBody As String
Dim intIn, intLength As Long
Dim intAttachCount As Integer, intStandardAttachCount As Integer
'
On Error GoTo ErrorHandler
'
'You may have a picture or vCard in your email signature that you
'don't want to be counted when checking for attachments. If so, then
'edit the following line to make intStandardAttachCount equal the
'number of files attached in your signature.
intStandardAttachCount = 0
'
'CHECK #1: Check for a blank subject line
If Item.Subject = "" Then
'Extra spaces added to the messages just to
'keep them centered and pretty
m = MsgBox("The subject line is blank... " & _
vbNewLine & vbNewLine & _
"Do you still want to send this message? ", _
vbYesNo + vbDefaultButton2 + vbExclamation + vbMsgBoxSetForeground, "Blank Subject")
If m = vbNo Then
Cancel = True
GoTo ExitSub
End If
End If
'
'CHECK #2: Check for a missing attachment
intIn = 0
strBody = LCase(Item.Subject) & LCase(Item.Body)
'If the message is a reply or forward, then the macro will
'not search for the strings in the original message. Anything
'below the "from:" line is ignored
intLength = InStr(1, strBody, "from:")
If intLength = 0 Then intLength = Len(strBody)
'
'Add lines for every string you want to check, including other
'languages, etc. Partial strings are fine. For example, "attach"
'will match "attached" & "attachment"
If intIn = 0 Then intIn = InStr(1, Left(strBody, intLength), "attach")
If intIn = 0 Then intIn = InStr(1, Left(strBody, intLength), "file")
If intIn = 0 Then intIn = InStr(1, Left(strBody, intLength), "enclosed")
'
intAttachCount = Item.Attachments.Count
If intIn > 0 And intAttachCount <= intStandardAttachCount Then
m = MsgBox("It looks like you forgot to attach a file... " _
& vbNewLine & vbNewLine & _
"Do you still want to send this message? ", _
vbYesNo + vbDefaultButton2 + vbExclamation + vbMsgBoxSetForeground, "Attachment Missing?")
If m = vbNo Then
Cancel = True
GoTo ExitSub
End If
End If
'
'CHECK #3: Check for meeting requests with no location
If Item.Class = olMeetingRequest Then
If InStr(1, Item.Body, "Where:", vbTextCompare) = 0 Then
m = MsgBox("The meeting location is blank... " _
& vbNewLine & vbNewLine & _
"Do you still want to send this meeting invite? ", _
vbYesNo + vbDefaultButton2 + vbExclamation + vbMsgBoxSetForeground, "Blank Location")
If m = vbNo Then
Cancel = True
GoTo ExitSub
End If
End If
End If
'
ExitSub:
Set Item = Nothing
strBody = ""
Exit Sub
'
ErrorHandler:
MsgBox "Send Checker" & vbCrLf & vbCrLf _
& "Error Code: " & Err.Number & vbCrLf & Err.Description
Err.Clear
GoTo ExitSub
End Sub
I know there are probably 100 variations of this sort of macro floating out on the web, but I’ve customized this one quite a bit and it really works well for me. Hopefully you can make some use of it too.
Another good way to prevent an “oh shit” moment is by delaying your outgoing messages by a few minutes, as described here and here. A nice twist to this if you want to send a particular message immediately is to add an exception to the Outlook rule, such as if the subject line contains two underscores “__”, or if the importance is set to high.
Good luck!
Carl



September 18, 2008 - 11:37 pm
do you know what will be timestamp of the recalled message?
September 19, 2008 - 7:30 am
Vixmus -
The macro above does not recall messages or use the Outlook recall message feature. It stops messages before they are sent in case you want to correct any of the missing items. The time stamp will still be whatever time you finally send the message.
Does that answer your question?
-Carl
September 29, 2008 - 10:43 am
Here’s one variation that includes some other features as well:
Outlook Etiquette Check VBA Code
November 24, 2008 - 12:05 pm
I’ve tried several different versions of the above code from around the net, but seemingly all of them have the same problem. When I send the email the box that asks whether I want to send the email or not pops up underneath the email itself, to wherever my inbox is. Is there a way to change this? Thanks!
November 25, 2008 - 1:13 pm
Matt – I have never experienced the issue you described. There are a few things you could try to force the window to the top. The properties of VBA message box are determined by a series of constants that get summed together. Each constant represents a different attribute. Look for the sections in the code that state something like “vbYesNo + vbExclamation”. Try adding either “vbMsgBoxSetForeground” or “vbSystemModal”. These are both valid attributes for the MsgBox command. For example, the line
vbYesNo + vbExclamation, “Attachment Missing?”)
Would be changed to
vbYesNo + vbExclamation + vbMsgBoxSetForeground, “Attachment Missing?”)
Please post a comment back here and let me know if it helped. Good luck!
-Carl
November 26, 2008 - 10:14 am
It worked, but strangely instead of just pulling up the option window it pulls up the entire inbox and the window. It’s not a big deal, you’re not searching for the box anymore; but I figured from a developers standpoint you might be interested to know that.
November 26, 2008 - 10:20 am
O and thanks a lot btw! This is a really helpful piece of code… I’ve gotten in trouble like 3 times at work for absent minded email omissions. They’re really anal about that in my office. Very helpful for avoiding that problem again. Thanks!
November 26, 2008 - 11:21 pm
Matt – Thanks for the feedback, glad to hear it helped.
-Carl
February 18, 2009 - 7:37 am
This is great but doesn’t check the subject line, if i send an email file 3 of 4 in subject line and nothing else. Is there a way this is possible? Thanks Ed.
February 22, 2009 - 3:10 pm
Ed – Good suggestion, I already updated the code above to include the subject line. It required changing just a single line in Check #2.
strBody = LCase(Item.Subject) & LCase(Item.Body)
Thanks!
-Carl
April 2, 2009 - 9:15 am
Macro has stopped working. Any updates as to why?
April 4, 2009 - 5:28 pm
Larry – You’ll have to give me a little more to go on. Are you launching the macro from a toolbar button? Do you get any error messages or pop-ups? What OS and what version of Outlook are you using? Do you have other macros that are working?
-Carl
May 18, 2009 - 4:35 pm
Outlook 2007 checks for no location on appointments by default so the macro is redundant. Since the code is well annotated the section can be deleted.
May 18, 2009 - 4:57 pm
Tammy – Good point, thanks for the comment. If you have OL2007, you can just delete the “CHECK #3″ section, everything before the “ExitSub:” line.
-Carl
May 20, 2009 - 4:58 pm
Hi Carl,
I have tried your macro with Outlook 2007 SP2 and it only worked once. After rebooting the PC, the macro stopped working.
I can make the macro work again by deleting the VbaProject.OTM at %appdata%\Microsoft\Outlook and make again the macro, but after rebooting it won’t work any more .
It is only working on the current session. If I restart the PC the macro does not work.
Any idea what’s happening?
Thank you.
May 20, 2009 - 9:04 pm
José – I’m not sure what would cause that, but I don’t have OL2007 so I’m not able to try it. Are you maybe having the issue that Matt described above where the pop-up message is hidden behind other windows?
When you say the macro stopped working, what do you mean exactly? Do you see error messages, or does it just let all messages pass without warning? Do you have other macros that are working, or do all of your macros stop working?
-Carl
May 21, 2009 - 2:35 am
Hi Marc,
No I don’t see any error messages. All messages pass without warnings. I have tried to change the code using the “vbMsgBoxSetForeground” or “vbSystemModal” options but the behaviour is the same.
And no, I don’t have any other macros.
I even have try this on other computer with Outlook 2007 and the behaviour is exactly the same. All messages pass without warnings after rebooting.
Thank for your help.
May 21, 2009 - 7:32 am
Sorry for misspelling your name… not Mark but Carl
May 21, 2009 - 9:03 pm
José -
No problem, people have called me much worse things! A few more things you can try:
[1] It might be related to your macro security setting. Try moving down to medium to see if it fixes the issue. I think that is in “Tools > Trust Center” for OL2007. You have to *completely* shut down Outlook and restart it after changing the security level for it to stay.
If you don’t want to leave the setting at medium, there are instructions here, here, and elsewhere online that show you how to certify your macro so it will run even when the security level is high.
[2] Try running some very basic macros and launching them manually (not automatically). Here is an example:
Sub HelloWorld()
MsgBox “Hola mundo”
End Sub
[3] If that works manually, now try getting it to trigger from the message send event. Note that you can only have one macro with this title in your “ThisOutlookSession” module, so you will have to remove the other one before testing this one:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox “Hola mundo”
End Sub
[4] It shouldn’t matter, but try deleting the “Private” qualifier on the first line of the macro.
Good luck, please post a note here if you figure it out.
-Carl
May 22, 2009 - 7:21 am
Thank you Carl,
You were right! The problem was related to macro security settings.
I have disabled the security selecting the option “No security check for all macros” and it worked.
Then, I have certificate the macro and selected the option “Warnings for signed macros; all unsigned macros disable”. At first Outlook run and first macro warning I choose to trust all macros signed by me. And that’s it, macro is working and no more security warnings.
Thank you for your help.
Regards,
José
June 10, 2009 - 4:40 am
Hi Carl,
Thanks for compiling all of them together in this one.
This looks great.
Suggestions:-
1.Please add ‘ + vbMsgBoxSetForeground’ in your code itself, so that we’ll get the prompt in the foregroud.
2.Please add the steps for signing a macro with selfsign utility.This will avoid the macro security issue.
http://msdn.microsoft.com/en-us/library/aa155754(office.10).aspx
June 14, 2009 - 9:48 pm
Mankidavu – Thanks for your comments… Very good point on signing the macro. I do this on my machine as well to be able to leave the security level in place without getting a bunch of warnings. I recommend that people follow the MSDN link in your comment above and use the instructions there.
Regarding the +vbMsgBoxSetForeground setting, I just updated it for the three checks in the macro. This is based on my comment above from 25 Nov 08, but now it is in the base code. Thanks for the suggestion!
-Carl
July 16, 2009 - 10:56 am
Hey Carl.
Awesome macro, thanks! One issue, it works fine during my session. But when I close Outlook and reopen it, the macro doesn’t work until I actually go into VB and look at it, then it starts working again.
Do you have any idea why this would be?
Thanks in advance.
July 17, 2009 - 1:00 pm
Al – Sounds like it might be related to your macro security settings. See my comment from May 21st, 2009 above and try some of the suggestions there.
-Carl
November 12, 2009 - 5:01 am
I’ve been using this script for some time now. Seems to be working ok, except for one thing: my outlook warns me everytime I send an e-mail that a script is trying to read the e-mailaddresses involved.
I’ve tried the self-signing above; but no improvement there.
Any ideas?
November 12, 2009 - 9:28 pm
Michiel – Assuming the signing of the macro is working, it sounds like it might be an issue with your security level settings. See the comments above on 21-May-09 and 22-May-09. Also see discussion on signing in comments from June 10th & June 14th above.
-Carl
February 19, 2010 - 10:45 am
Hi Carl!
Thanks for a great code. This was working on my Outlook for a few days, and then all the sudden stopped working. Nothing pops up at all. I’ve brought my security level down to Low, tried deleting and re-programming the macro, and nothing seems to work. Any words of wisdom? I have Outlook 2003.
Thanks!
February 23, 2010 - 11:00 pm
Hi Rachel – So far, the most common issue for people has been their macro security level. If you’ve already ruled that out, I had a few other things to try in my comments from May 21st above. Basically, see if you can get *any* macro to work, starting with a simple message box, then move up from there. Good luck!
-Carl
March 9, 2010 - 9:04 am
Hi Carl,
Great Macro thanks.
Can you propose a way to help with the following
Due to the Outlook Auto Complete feature, I sometimes send an email to the wrong contact
And It can be very problematic at times…
I would like to write a macro that will do:
pop up a warning with a list of domains that the mail is sent to
or check that the mail is sent to an external domain (not my company domain name)
Thanks,
Amit
March 10, 2010 - 10:24 pm
Hi Amit -
First of all, if there are individual entries in your auto-complete addresses that you don’t want, you can remove them. Start typing in the “To” field until the unwanted name shows up. Then, highlight it (without clicking) and press the delete key.
You can also check domains for each outgoing message by modifying the macro. For example, to display the domains you could add code like:
If Item.Class = olMail ThenMsgBox Item.To & vbCrLf & Item.CC
End If
You can use the InStr command to check those fields for certain strings and trigger the popup based on that.
Happy coding!
-Carl
April 26, 2010 - 8:41 am
Hi Carl,
I use my outlook 2007 for work, uni and personal email and sometimes forget to change the ‘from’ line of my emails. Could you please give advice on how to write a macro so it will remind me each time I want to send an email as to whether I want to put one in? I’ve tried editing existing code like yours above, but can’t get it to work. (I’m not that great at programming) Any help would be gratefully received, thanks so much.
Harry
May 4, 2010 - 10:28 pm
Hi Harry – Any field that can be accessed by VBA can be checked. Once you find the field your want to check, you can re-use the Subject line check code (Check #1) to check for a blank From field. I think the field you want to check is Item.SenderEmailAddress or Item.SenderName, but that’s a guess on my part
-Carl
February 24, 2011 - 6:28 am
Hi Mark
Suppose if my email signature contains the word “attachments” then how I can modify above macro to suit my problem ??
February 24, 2011 - 9:03 am
You can have the macro stop short and not check your signature block. The macro is already set up to stop short if it detects “from:” in the message. Nothing is checked below that point because it is assumed to be part of a previous message. If you have a unique phrase at the beginning of your signature block you could add a line to the code:
intLength = InStr(1, strBody, “this is how my signature block starts”)
intLength = InStr(1, strBody, “from:”)
If you are using your signature block on replies and forwards it will get a little more complicated since you want to make sure that the smaller of the two numbers gets assigned to intLength. You could do something like:
intLength1 = InStr(1, strBody, “this is how my signature block starts”)
intLength2 = InStr(1, strBody, “from:”)
…and then compare them and assign the smaller one to intLength. You should also add any new variables to the declaration block at the top of the macro.
Hope that helps.
-Carl
February 26, 2011 - 4:21 am
Hi Carl
Thanks for sharing above info , but I’m still struggling to get this macro to work as the word “attachment” in my email signature is still caught by this macro after your mentioned changes .
If you find any other solution please share on this forum
Have a nice day
[:)]
February 27, 2011 - 3:27 pm
shashank -
I just tested it to be sure, and I was able to get this to work in OL2010 with no problems. I used the signature block “My legal team makes me talk about attachments in my signature block.” The modified macro only checked up to the signature block and did not check the signature block itself.
A few tips…
(1) Note that the strBody variable is what gets searched, and everything is converted to lower case. That means the following line with the InStr() function needs to search for lower case.
(2) Rather than just searching for the word “attachments”, you should search for a longer phrase that indicates the beginning of your signature block. In the example above, I searched for the phrase “my legal team makes me”.
(3) Insert a MsgBox intLength line to debug the macro immediately after intLength is assigned and see what value is assigned to intLength when the macro runs. If it is zero, then it is scanning the whole message (including signature blocks).
(4) The order of how you assign intLength matters since it is being overwritten. You can try swapping the order, or to be absolutely sure you can use separate variables (and keep the smaller one) as I suggested in my previous comment.
Hope that helps.
-Carl
February 28, 2011 - 3:59 am
Hi Carl
Thanks for sharing important tips .
Now I figured out where I was committing mistake. Now the macro is working according to my wish . Again Thanks a ton !!
have a nice day
March 15, 2011 - 2:04 pm
Hi Carl,
Is there a difference between files that you attach and files (images) attached in body message?
I have to discard files attached in body message or to know when I attach a file that not belong to he message history.
Thanks in advance.
Best Regards.
Manuel
August 20, 2011 - 4:42 pm
Manuel –
Those will likely show up as different attachment types. I have another macro to actually remove attachments (see this post). You will notice in that macro there are several lines that check the attachment type, such as “If objAttachments.Item(1).Type <> olOLE Then…” You could use similar logic for your application.
-Carl
May 18, 2011 - 5:43 am
A very helpful Macro…….
Many thanks,
May 24, 2011 - 7:16 am
Hi Carl,
Have you ever tried to change the tabs and add macros to the outlook task? I use task and shared tasks a lot and I added few more tabs which will cover the meeting notes, communications and history. But my main issue is to add/insert the files/attachements and ability to put some macros in place to do the calculations for budget or time lines. appreciate any suggestions.
Take care,
Rose
August 20, 2011 - 4:45 pm
Hi Rose –
The Task body in Outlook is a little tricky to modify using VBA since it is in RTF format. I’m not sure why Microsoft decided to make tasks different in that regard from the other common objects in Outlook. Having said that, you can definitely add attachments manually via drag & drop.
-Carl
June 8, 2011 - 9:40 pm
Carl,
This works so great for me, thanks for this great share!
Marco
June 20, 2011 - 12:28 pm
Carl,
This was working great for me for several years on 2003 and 2007. I recently upgraded to Outlook 2010 and Check #2 for the attachments is not working properly. If I have a brand new email it works find. However, if I reply to/forward someone’s email, the check is not working properly for me. I have had several instances where I was replying with an attached file but forgot to attach the file.
I am such a newbie when it comes to the VBA code, I don’t know what to edit to ensure this check starts working again.
Thanks,
Keith
August 20, 2011 - 5:10 pm
Hi Keith –
I have been using the macro with Outlook 2010 for ~8 months now and I haven’t had any issues. Note that for replies & forwards, Check #2 will only look at text that is above the “From:” line. The reason is that if someone sends you a mail with an attachment with a message like “Here is the file”, and you send a short reply like “Thanks” (with no attachment) you don’t want the macro to trigger and give you a pop-up. It should only trigger if the word “file” is in your response (i.e. above the “From:” line).
By the way, for OL2010, you can delete or disable Check #3 since OL2010 now has this check built in.
I also added a Check #4 for Outlook 2010 since when I forward a meeting invite to someone I don’t always want to send a notification to the original meeting owner. Outlook 2010 normally does this automatically without your consent (check your Sent Items after forwarding a meeting invite!). I will post an updated macro with Check #4 when I get a chance.
-Carl
July 6, 2011 - 10:00 am
Hi Carl,
Thank you for this useful macro.
One additional check I particularly like using is to prevent sending emails without any values in the “TO” field.
I recently changed jobs. The previous company I worked for had Outlook configured that way as a default, but in the new company Outlook allows me to send emails without anyone on the “TO” field which really annoys me…
If any one knows of a way of preventing this by Outlook configuration please let me know. In the meantime I have achieved the goal via this additional check on the macro:
‘CHECK #4: Check for an empty “TO” parameter
If Item.To = “” Then
‘Extra spaces added to the messages just to
‘keep them centered and pretty
m = MsgBox(“There are no recipients defined for this email, please enter a valid recipient”, _
vbOK + vbDefaultButton1 + vbExclamation + vbMsgBoxSetForeground, “Blank Recipient”)
Cancel = True
GoTo ExitSub
End If
August 20, 2011 - 5:13 pm
Diana –
Nice addition – thanks for sharing! I think Outlook allows this because sometimes people want to send out messages to large groups but not disclose the recipients. In that case you would put everyone in the BCC line and nobody in the TO line. Of course, you could use logic in the check to warn the user but then give them the option to send or cancel.
-Carl
August 31, 2011 - 7:21 am
If you change
strBody = LCase(Item.Subject) & LCase(Item.Body)
to
strBody = LCase(Left(Item.Subject, (Len(Item.Subject) * Abs((InStr(2, Item.Subject, “:”) 3))))) _
& LCase(Item.Body)
In the Attachment check (#2) it won’t check the Subject Line for replies/forwards (assumes a “:” in third character indicates this)
September 1, 2011 - 9:28 pm
Hi Tom –
Thanks for sharing this… If I understand this correctly, you are trying to prevent a “false positive” trigger caused by text in a reply/forward subject. For example, if someone sends you an attached file with a subject like “Here’s the file”, and you reply back with a simple “Thanks!”, Check #2 would have triggered on your reply with a subject of “RE: Here’s the file”. With the new code, the subject line is not included in the check.
The risk, however, is that if you use a colon “:” elsewhere in your subject line on a regular message (non-forward/non-reply) the extra code could produce some unexpected results. (I didn’t test this). If that’s the case, I would rather live with the occasional false positive since the cost is extremely low (i.e. a pop-up asking if I’m sure I want to send).
Just my two cents.
-Carl
September 5, 2011 - 8:07 am
Hi Carl, thanks for responding.
The code I changed only makes a difference if the 3rd character is a colon.
It seemed easier than checking for RE, or FW strings in various cases and I couldn’t imagine why the third character would be a colon otherwise.
Having said that it is really ugly and it only saves the occasional click-through. It just happened to be an issue with the very first email I sent after adding your code.
Forgot to say thanks for posting in my previous comment, it’s been useful already!
December 23, 2011 - 5:54 pm
Got it – Thanks for sharing your mods!
-Carl
October 13, 2011 - 12:51 pm
I want a macro which should return error msg if a particular tetx or number is missing in the subject field,,Eg.If 298 is missing in the subject field,i shud get a msg”298 is missing in the subject do u want to send anyway”
can anyone plz help me!!!!!!
December 23, 2011 - 6:16 pm
You could definitely use “Check #2″ above as a starting point. Just reverse the logic to look for the absence of something in the subject line. Since you only want to check the subject and not the body, change
strBody = LCase(Item.Subject) & LCase(Item.Body)
to:
strBody = LCase(Item.Subject)
and remove the next few lines that are looking for the word “from:” in the body. Then, remove the other word checks that are looking for “file”, “attach”, etc. and just have one that contains your string:
If intIn = 0 Then intIn = InStr(1, strBody, “298″)
With this logic, a value of 0 should trigger a message, so change:
If intIn > 0 And intAttachCount <= intStandardAttachCount Then
to this:
If intIn = 0 Then
Note that you can remove the references to attachment counts if you are not using them.
There may be a few other changes required as well, but what you are asking can definitely be done. The best way to learn is to do some hands-on experiments
Hope that helps!
-Carl
October 24, 2011 - 6:45 am
Great code, very useful for work e-mails.
Is there anyway when a pop up box comes up and you wish to cancel sending the e-mail to have the draft e-mail open on the desktop? it always gets minimized to the toolbar and I have to re-click to open
Small complaint I know
December 23, 2011 - 6:25 pm
Hi Sean – I am not seeing that behavior. I’m not sure why it would minimize the draft. On both of my systems (Outlook 2003 and Outlook 2010) it always left the draft window in open / in place. Which OS and which version of Outlook are you running?
-Carl
November 14, 2011 - 10:14 am
I have an image in the signature. It is only when i am sending a new mail the signature is inserted.
However when i am replying to anybody’s mail the signature is not present.
Is there any way by which the Macro can detect whether my signature contains an image or not.
I don’t want to keep on changing the attachment count in macro based on scenario.
Also some people may or may not have attachments while replying. So is there a way that can work for all sort of scenarios.
December 23, 2011 - 6:33 pm
Hi Anup –
I think you can do this, and it would be a nice enhancement to the macro. You can probably detect (and ignore) your own signature attachment using the attachment size, type (OLE, etc.), or exact filename. If you look at my code for the Outlook attachment stripper, you will see some examples of how to check for these parameters. For example, I look for an attachment named “Attachments Removed” in that macro and skip the message if I find it.
-Carl
December 13, 2011 - 12:59 am
Thanks a ton for this blog. I’ve got a slightly different use case which I’ve got working. I want to confirm (via message box) that every email that has an attachment has the text [SD] in the subject line. If [SD] is missing from emails with attachements, I want the messagebox to act as a confirmation. If there’s no attachement, then I don’t care what’s in the subject line. The code I have is working fine. The only problem is it gives a false positive if there is an image or picture in the body of the email (or forward/reply). I tried to adapt some of your principles in this post but couldn’t get it to work. Any chance you could lend a hand? Thanks again and God bless.
-Joshua
Here’s my code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
strSubject = Item.Subject
Dim intAttachCount As Integer
intAttachCount = Item.Attachments.Count
If intAttachCount > 0 Then
If InStr(strSubject, “[SD]“) = 0 Then
Prompt$ = “Email has an attachment and is not being sent via Secure Delivery. Are you sure you want to send the email?”
If MsgBox(Prompt$, vbYesNo + vbCritical + vbMsgBoxSetForeground, “Secure Delivery Check”) = vbNo Then
Cancel = True
End If
End If
End If
End Sub
December 23, 2011 - 6:41 pm
Joshua –
If I understand correctly, you don’t want to trigger on embedded (OLE) attachments. Once you detect that the attachment count is > 0 you would have to step through each attachment to see if it is OLE vs. something else. You could re-use some of the code in the Outlook attachment stripper to check this. Look for lines like “If objAttachments.Item(1).Type <> olOLE Then”
You might want to also alter the order of your checks in that case (check for attachments > 0 first, then check to see if “[SD]” is missing, then (if missing) step through the attachments.
Hope that helps!
-Carl
February 3, 2012 - 4:08 am
Thanks for the post. I see that it’s been helping a lot of us for more than 3 years.
I was wondering if there’s a way to retain this code between sessions. Something like a start-up batch that attaches this code to the current outlook session? What happens if i restart my system or something?