Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / New Users / May 2008

Tip: Looking for answers? Try searching our database.

Outlook macro not working in Enterprise 2007

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robeyx - 23 May 2008 10:11 GMT
I have a macro that deletes mail in the junk folder, depending on various
conditions I set.  It works fine in Outlook 2003 but seems to make Outlook
Enterprise 2007 hang.

It looks like this and any help would be much appreciated:

Public WithEvents myOlItems As Outlook.Items

Public Sub Application_Startup()
   Dim myOlApp As New Outlook.Application
   Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderJunk).Items
   Set myInItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
   Dim myOlApp As Outlook.Application
   Dim junkFolder, deletedItemsFolder  As Outlook.MAPIFolder
   Dim junkItem, deleteItem            As Object
   Dim pos, count                      As Integer
   Set deletedItemsFolder = Application.GetNamespace("MAPI").
GetDefaultFolder(olFolderDeletedItems)
   Set junkFolder = Application.GetNamespace("MAPI").GetDefaultFolder
(olFolderJunk)

' Specify all the words to check for in the subject or body and we'll delete
the email if found
   wordCheck = Array("accessories", "end")

   For Each junkItem In junkFolder.Items  
       On Error Resume Next              
' Check the Subject and Body for all the words we specified in the Array
checkWords:
       pos = InStr(1, junkItem.Subject, wordCheck(count), 1)   ' Is the
string in the Subject?
       If pos > 0 Then GoTo deleteJunkItem                     ' Yes, go
delete it
       pos = InStr(1, junkItem.Body, wordCheck(count), 1)      ' Is the
string in the Body?
       If pos > 0 Then GoTo deleteJunkItem                     ' Yes, go
delete it
       count = count + 1
       If wordCheck(count) <> "end" Then GoTo checkWords       ' Loop back
if there's more words
       GoTo doNext

deleteJunkItem:
       entryID = junkItem.entryID                              ' Store item
entry id
       junkItem.Delete                                         ' Move the
item to the Deleted Items folder

doNext: Next                                                    ' Go look at
the next item in Junk Mail
   
' Now delete everything in the Deleted Items folder, which means anything I
deleted manually too
   For Each Item In deletedItemsFolder.Items
       Item.Delete
   Next

   Set junkItem = Nothing
   Set junkFolder = Nothing
   Set deleteItem = Nothing
   Set deletedItemsFolder = Nothing

End Sub
Ken Slovak - [MVP - Outlook] - 23 May 2008 13:47 GMT
You shouldn't declare an Outlook.Application object, use the intrinsic
Application object instead, it's a trusted object. When deleting objects
from a collection in a loop use a down-counting For loop, not For Each or an
up-counting loop.

Have you set a breakpoint in your code to see how it runs when you step
through it? See what happens.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm

>I have a macro that deletes mail in the junk folder, depending on various
> conditions I set.  It works fine in Outlook 2003 but seems to make Outlook
[quoted text clipped - 66 lines]
>
> End Sub
Robeyx - 23 May 2008 14:16 GMT
Many thanks Ken.  This is my first post to OfficeKB, so I'm delighted at the
result.

I'll change the loop style and find out what an intrinsic object is, and I'll
put some break points in too.

Cheers
Frank

>You shouldn't declare an Outlook.Application object, use the intrinsic
>Application object instead, it's a trusted object. When deleting objects
[quoted text clipped - 9 lines]
>>
>> End Sub
Ken Slovak - [MVP - Outlook] - 23 May 2008 16:06 GMT
An intrinsic object is one that's already there for you, you don't do
anything. In Outlook VBA code Application refers to Outlook.Application, so
you never need to instantiate an object like that. You just use Application
wherever you'd use an Outlook.Application object. The advantage is that
Application object is trusted by Outlook and won't fire the security.

Signature

Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm

> Many thanks Ken.  This is my first post to OfficeKB, so I'm delighted at
> the
[quoted text clipped - 6 lines]
> Cheers
> Frank
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.