Trapping AttachmentAdd in VBA does not handle multiple email instances

Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
Starting with example code shown at http://msdn.microsoft.com/en-us/library/office/aa209975(v=office.11).aspx, I am able to create a warning when attempting to add an attachment greater than a certain size. And this works great. However, when I have multiple emails open, it only works with the most recently opened email and does not work for any other open email. From what I understand, the problem is with the way I "Set newItem". After researching this problem, it looks like I need a way to handle multiple open inspectors.

What is the simplest way to accomplish this? I've found references to an "inspector wrapper" as well as links to examples. But going through the examples, and I'm a beginner with programming, these appear to be overkill for what I'm trying to do since I do not need to do anything with commandbarbuttons or contact items.

Please advise and point me in the right direction. Thanks.

Code

Code:
Public WithEvents goInspectors As outlook.Inspectors 
 
Public WithEvents newItem As outlook.MailItem 
 
Private Sub Initialize_Handlers()
   Set goInspectors = outlook.Application.Inspectors 
 
End Sub 
 
Private Sub Application_Startup()
   Initialize_Handlers 
 
End Sub 
 
Private Sub goInspectors_NewInspector(ByVal Inspector As Inspector)
   If Inspector.CurrentItem.Class = olMail Then
       Set newItem = Inspector.CurrentItem
   End If 
 
End Sub 
 
Private Sub newItem_AttachmentAdd(ByVal newAttachment As Attachment)
   If newAttachment.Type = olByValue Then
       newItem.Save
       If newItem.Size > 500 Then '500 bytes used for testing purposes only
           MsgBox "Warning: Item size is now " & newItem.Size & " bytes."
       End If
   End If 
 
End Sub
Links to Inspector Wrappers

Code:
http://www.slovaktech.com/code_samples.htm#InspectorWrapper 
 
http://www.outlookcode.com/codedetail.aspx?id=1344
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Thread starter Similar threads Forum Replies Date
J Outlook VBA and Custom Forms 1
C Outlook VBA and Custom Forms 2

Similar threads

Top