WithEvents ??



I want the code to run everytime a message come in. How
do i do this or is it even possible?
What i have is a non default folder in outlook 2000
and i'm using Sue function. Any Help is appreciated


Private Sub Application_Startup()
End sub

Sub mySpammerDel()
Dim objFolder
Dim myItems, myItem
Dim i
Set objFolder = GetFolder("Personal Folders\Spam")
myItems = objFolder.Items.Count
For i = myItems To 1 Step -1
Set myItem = objFolder.Items(i)
Next i
Set objFolder = Nothing
End Sub

Public Function GetFolder(FolderPath)
' folder path needs to be something like
' "Public Folders\All Public Folders\Company\Sales"
Dim aFolders
Dim fldr
Dim i
Dim objNS
Dim strFolderPath

On Error Resume Next
strFolderPath = Replace(FolderPath, "/", "\")
aFolders = Split(FolderPath, "\")

'get the Outlook objects
' use intrinsic Application object in form script
Set objNS = Application.GetNamespace("MAPI")

'set the root folder
Set fldr = objNS.Folders(aFolders(0))

'loop through the array to get the subfolder
'loop is skipped when there is only one element in the
For i = 1 To UBound(aFolders)
Set fldr = fldr.Folders(aFolders(i))
'check for errors
If Err <> 0 Then Exit Function
Set GetFolder = fldr

' dereference objects
Set objNS = Nothing
End Function

Michael Bauer

Hi Mark,
I want the code to run everytime a message come in.

Add this:

Private WithEvents SpamItems As Outlook.Items

Private Sub Application_Startup()
On Error Resume Next
Set SpamItems = GetFolder("Personal Folders\Spam").Items
End Sub

Private Sub SpamItems_ItemAdd(ByVal Item As Object)
End Sub


Thanks alot Michael

Works Great, Really Appreciate your help.
I learned something from your code.

Thanks Again

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