A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook - General Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Outlook macro not working in Enterprise 2007



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 23rd 08, 10:11 AM posted to microsoft.public.outlook
Robeyx
external usenet poster
 
Posts: 2
Default Outlook macro not working in Enterprise 2007

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).Ite ms
Set myInItems = Outlook.Session.GetDefaultFolder(olFolderInbox).It ems
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

Ads
  #2  
Old May 23rd 08, 01:47 PM posted to microsoft.public.outlook
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Outlook macro not working in Enterprise 2007

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.

--
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


"Robeyx" u43777@uwe wrote in message news:8492f3dae590d@uwe...
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).Ite ms
Set myInItems = Outlook.Session.GetDefaultFolder(olFolderInbox).It ems
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


  #3  
Old May 23rd 08, 02:16 PM posted to microsoft.public.outlook
Robeyx
external usenet poster
 
Posts: 2
Default Outlook macro not working in Enterprise 2007

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

Ken Slovak - [MVP - Outlook] wrote:
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.

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


  #4  
Old May 23rd 08, 04:06 PM posted to microsoft.public.outlook
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Outlook macro not working in Enterprise 2007

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.

--
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


"Robeyx" u43777@uwe wrote in message news:849516a56d327@uwe...
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


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook 2007 MS Enterprise uninstall reinstall garyaw Outlook - Installation 1 February 18th 08 08:34 PM
can you add yahoo mail to office 2007 enterprise willie75253 Outlook - General Queries 1 November 18th 07 07:27 AM
Installed Office Enterprise 2007. Outlook will open only in safe Burl Morris Outlook - Installation 0 November 12th 07 06:21 PM
outlook 2007 enterprise downloaded mail - inbox empty except searc Pete R Outlook - Installation 0 October 7th 07 05:56 PM
OutLook 2003 & Exchange 2007 Enterprise Larry Bird Outlook - Installation 4 December 29th 06 03:27 PM


All times are GMT +1. The time now is 08:12 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2024 Outlook Banter.
The comments are property of their posters.