WithEvents ??

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

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

Thanks
Mark

Private Sub Application_Startup()
mySpammerDel
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)
myItem.Delete
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
array
For i = 1 To UBound(aFolders)
Set fldr = fldr.Folders(aFolders(i))
'check for errors
If Err <> 0 Then Exit Function
Next
Set GetFolder = fldr

' dereference objects
Set objNS = Nothing
End Function
 
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)
mySpammerDel
End Sub
 
Thanks alot Michael

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

Thanks Again
Mark
 
Back
Top