AttachmentAdd Event

R

Richard Quick

Hi,

Can anyone explain to me how I would use the following code found in the
Outlook VBA help.

I have copied the code in to the project as explained and closed the VBA
editor, when I add an attachment I do not get a warning.

I am aiming to use this Event to warn if attachments are sent from certain
directories if this is possible. Can anyone put me on the right track?

Thanks in advance.
Richard




------------------------------------
Attachment Event
See AlsoApplies ToExampleSpecifics
Occurs when an attachment has been added to an item.

Sub object_AttachmentAdd(Attachment As Attachment)

object An object that evaluates to one of the objects in the Applies To
list. In Microsoft Visual Basic Scripting Edition (VBScript) in an Outlook
form, use the word Item.

Attachment Required. The Attachment that was added to the item.

Example
This Visual Basic for Applications (VBA) example checks the size of the item
after an attachment has been added and displays a warning if the size
exceeds 500,000 bytes. The sample code must be placed in a class module such
as ThisOutlookSession, and the TestAttachAdd() procedure should be called
before the event procedure can be called by Microsoft Outlook.

Public WithEvents newItem As Outlook.MailItem

Private Sub newItem_AttachmentAdd(ByVal newAttachment As Attachment)
If newAttachment.Type = olByValue Then
newItem.Save
If newItem.Size > 500000 Then
MsgBox "Warning: Item size is now " & newItem.Size & " bytes."
End If
End If
End Sub

Public Sub TestAttachAdd()
Dim olApp As New Outlook.Application
Dim atts As Outlook.Attachments
Dim newAttachment As Outlook.Attachment

Set newItem = olApp.CreateItem(olMailItem) newItem.Subject
= "Test attachment"
Set atts = newItem.Attachments
Set newAttachment = atts.Add("C:\Test.txt", olByValue)
End Sub
This VBScript example shows how to use the AttachmentAdd event in
VBScript.

Sub Item_AttachmentAdd(ByVal newAttachment)
If newAttachment.Type = 1 Then
Item.Save
If Item.Size > 500000 Then
MsgBox "Warning: Item size is now " & Item.Size & " bytes."
End If
End If
End Sub
 
S

Sue Mosher [MVP-Outlook]

I don't see any statement to instantiate NewItem. You can use the Inspectors.NewInspector event to do that.

In any case, though, what you're trying to do isn't possible. Outlook doesn't provide any way of knowing the original source directory of an attachment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top