PDA

View Full Version : Solved: Application.Itemsend not working?



tpoynton
09-23-2006, 09:35 AM
Greetings,

I forget to send attachments way too often, so while I was trying to figure out how VBA could help me, I stumbled upon this at sourceforge.net. (http://sourceforge.net/projects/checkattachment)

I noticed that the variables were not declared, so I changed that to the best of my ability (I know using Variant is frowned upon; what is the correct type for those below?)

I also noticed that it didnt check the subject line, so I added that.

Everything worked great until I restarted Outlook. Now it it seems that the macro doesnt even 'fire' when an email is sent (tried having a messagebox appear). again, all worked well until i restarted outlook!

This code is in the ThisOutlookSession. I have never tried to do anything with VBA in Outlook, so I suspect I am missing something silly...

Also, I am using Outlook 2003 - THANKS! tim


Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Rem=======================================================
Rem
Rem PROJECT: CheckAttachment
Rem Fairtec (www.fairtec.at (http://www.fairtec.at))
Rem
Rem COPYRIGHT Manfred Hofbauer FAIRTEC
Rem LICENSE GNU/PL
Rem
Rem AUTHORS: MHO: Manfred Hofbauer (opensource@fairtec.at)
Rem DESCRIPTION: a simple macro, that checks if you mentioned an attachment and
Rem didn't attach it to your mail
Rem
Rem Subversion Tags
Rem
Rem $Author: Manfred.Hofbauer $
Rem $LastChangedDate: 2005-08-26 10:00:38 +0200 (Fr, 26 Aug 2005) $
Rem $LastChangedRevision: 663 $
Rem $LastChangedBy: Tim Poynton $
Rem $Id: MacroCodeEN.txt 663 2005-08-26 08:00:38Z Manfred.Hofbauer $
Rem========================================================

Rem Code is just executed if it is a mail and there is nothing attached
If Item.Class = olMail And Item.Attachments.Count = 0 Then

'MsgBox ("macro works")
Dim arrKeywords As Variant
Dim sBoxQuestion As String
Dim sBoxTitle As String
Dim sMailText As String
Dim vAnswer As Variant
Dim sSubjectText As String
Dim i As Integer

Rem define keywords
arrKeywords = Array("ENCLOS", "ATTACH", "ENCLOSURE")
sBoxQuestion = "Shall the eMail be sent without an attachment " & _
"(an attachment was mentioned)?"
sBoxTitle = "Attachment Missing?"
sMailText = UCase(Item.Body)
sSubjectText = UCase(Item.Subject)

Rem compare mail-body with keywords
For i = 0 To UBound(arrKeywords)
If InStr(sMailText, arrKeywords(i)) > 0 _
Or InStr(sSubjectText, arrKeywords(i)) > 0 Then
vAnswer = MsgBox(sBoxQuestion, vbYesNo & vbDefaultButton2, sBoxTitle)
If vAnswer = vbNo Then
Cancel = True
Exit For
End If
End If
Next i
End If

End Sub

tpoynton
09-23-2006, 09:43 AM
OK...I wasnt prompted with the 'enable macros' query when I restarted; now I am, and it seems to work.

Is there no facility to create 'add ins' for outlook? I will certainly follow the steps to self-sign in the Articles section to do that for myself, but is there an easier way for other people to do this? THANKS!

EDIT:

OK, I am pretty happy with how this works now. Some 'improvements' over the original code are that this gives the messagebox focus when it appears (previously it was often behind something), then returns focus to the email message if 'no' is selected from the message box. I'd still love to know how to improve this (particularly the use of variants) if someone is interested! tim


Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Rem========================================================
Rem
Rem PROJECT: CheckAttachment
Rem Fairtec (www.fairtec.at (http://www.fairtec.at))
Rem
Rem COPYRIGHT Manfred Hofbauer FAIRTEC
Rem LICENSE GNU/PL
Rem
Rem AUTHORS: MHO: Manfred Hofbauer (opensource@fairtec.at)
Rem DESCRIPTION: a simple macro, that checks if you mentioned an attachment
Rem and didn't attach it to your mail
Rem
Rem Subversion Tags
Rem
Rem $Author: Manfred.Hofbauer $
Rem $LastChangedDate: 2005-08-26 10:00:38 +0200 (Fr, 26 Aug 2005) $
Rem $LastChangedRevision: 663 $
Rem $LastChangedBy: Tim Poynton $
Rem $Id: MacroCodeEN.txt 663 2005-08-26 08:00:38Z Manfred.Hofbauer $
Rem======================================================

Rem Code is just executed if it is a mail and there is nothing attached
If Item.Class = olMail And Item.Attachments.Count = 0 Then

'MsgBox ("macro works")
Dim arrKeywords As Variant
Dim sBoxQuestion As String
Dim sBoxTitle As String
Dim sMailText As String
Dim vAnswer As Variant
Dim sSubjectText As String
Dim oMail As Outlook.MailItem
Dim i As Integer

Rem define keywords
arrKeywords = Array("ENCLOS", "ATTACH", "ENCLOSURE")
sBoxQuestion = "It appears as though an attachment is mentioned in your message." _
& vbCrLf & vbCrLf & "Should this message be sent without an attachment?" & _
vbCrLf & "Click 'Yes' to send immediately, or 'No' to add your attachment"
sBoxTitle = "Attachment Missing?"
sMailText = UCase(Item.Body)
sSubjectText = UCase(Item.Subject)

Rem compare mail-body with keywords
For i = 0 To UBound(arrKeywords)
If InStr(sMailText, arrKeywords(i)) > 0 _
Or InStr(sSubjectText, arrKeywords(i)) > 0 Then
vAnswer = MsgBox(sBoxQuestion, vbYesNo + vbSystemModal + vbDefaultButton2, sBoxTitle)
If vAnswer = vbNo Then
Cancel = True
With oMail
Item.Display
End With
Exit For
End If
End If
Next i
End If

End Sub

TrippyTom
01-08-2007, 11:51 AM
Hey Tim,
How did you force it to enable macros? I was using this code on Friday at work and was LOVING IT! But now it's not firing on ItemSend. I can't live without this! :)

Can you tell me how you got it to fire on the send event?

tpoynton
01-08-2007, 12:12 PM
I've since switched to Thunderbird, which has a nice 'withAttach' extension!

I did a little bit of a search on google, and one reference says (for an unrelated macro) that you should compile the code before restarting outlook.

I dont remember exactly how I got it to work; I think I was prompted after rebooting, but I honestly dont recall. I tried to take a look at my own installation, and now I can not even get to the VBE or the security settings under the Macro item in the tools menu. Weird.

I apologize for being pretty useless here...good luck! tim

TrippyTom
01-08-2007, 12:26 PM
Hmm....
Unfortunately I can't go installing other programs on my PC at work. And I've tried rebooting, compiling and restarting Outlook, but it's still not working. :(

I hope someone can shed some light on this minor annoyance.

tpoynton
01-08-2007, 12:33 PM
MWE and/or Mathewspatrick also developed similar code - see this entry (http://www.vbaexpress.com/forum/showthread.php?t=9996) in the potential KB's section