![]() |
|
|
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. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 |
|
|||
|
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 > |
|
|||
|
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 |
|
|||
|
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 |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|