Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > MS Office > Outlook

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-2008, 10:11 AM
Robeyx
 
Posts: n/a
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

Reply With Quote
  #2 (permalink)  
Old 05-23-2008, 01:47 PM
Ken Slovak - [MVP - Outlook]
 
Posts: n/a
Re: 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
>


Reply With Quote
  #3 (permalink)  
Old 05-23-2008, 02:16 PM
Robeyx
 
Posts: n/a
Re: 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


Reply With Quote
  #4 (permalink)  
Old 05-23-2008, 04:06 PM
Ken Slovak - [MVP - Outlook]
 
Posts: n/a
Re: 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


Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > MS Office > Outlook


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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:55 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Hookah | Gift Ideas | Loan | Buy Anything On eBay | Mortgage Calculator



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114