Link to home
Start Free TrialLog in
Avatar of jbreg
jbreg

asked on

Rule to Direct Mail to folder between 9-5, else to Inbox

I am trying to create an outlook rule to move messages from a specific sender and with specific words in the subject to a specific outlook folder I have created when they are receieved from 9-5. Otherwise, I want them to come into the inbox.

I realise I will have to write a VBA script but have no experience. I found https://www.experts-exchange.com/questions/20446440/E-mail.html but it doesn't deal with moving to a folder (rather than redirecting, and also doesn't tell me how to do the "from a specific sender" and "with specific words in the subject"

Can anyone help?
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbreg
jbreg

ASKER

Runtime error -2147221233

The Operation Failed. An object could not be found.

Debugging reveals this is the problem:

Set flr = ns.Folders(szDir)

BTW, in what format do i specify folder name, just foldername or /inbox/foldername?

The error is alomst certainly a result of the folder path being wrong.  The folder path must begin with a \ and is the path to the fodler beinging with the mailbox name.  For example, if my mailbox name is Mailbox, and the folder I want to put the messages in is FolderX, and FolderX is under Inbox, then path would be \Mailbox\Inbox\FolderX
Avatar of jbreg

ASKER

Still a little Lost, we're running exchange and in the left pane it says Mailbox - Example P. Person

I put in \Example P. Person\inbox\folder but get the same error?
Try using \Mailbox - Example P. Person\Inbox\Folder
Avatar of jbreg

ASKER

Tried that, now a different result--> first it appears to crash outlook. Then, it recovers with Run time error '13' type mismatch
Set olMailItem = olItem is flagged in debug.
Hmmm ...... ok, let's change this line

    olMailItem As MailItem, _

to

    olMailItem As Object, _
Avatar of jbreg

ASKER

Runtime error 4096
One or more items in the folder you synchronised do not match. To resolve the conflicts, open the items, and try this operation again.

Same type of error as before (appears to crash).

Also, how might I make the rule not apply on weekends--e.g. on weekends all would go into inbox as well?
Avatar of jbreg

ASKER

bolSenderMatch = (olMailItem.SenderName = "xxx@yy.com")

This is the line that gets flagged in debug.
Sorry, I've never seen that error before and have no idea why testing the senders name against a litteral value would have anything to do with synchronizing something.  What version of Outlook are you using?  If it's Outlook 2003 try turning off Cached Exchange Mode.

Let's get the macro to working first and then I'll add another test for weekends.
Avatar of jbreg

ASKER

Same exact errror without cached exchange mode (outlook 2003). Found this forum on 4096 errors http://www.outlookcode.com/threads.aspx?forumid=2&messageid=1268 but can't understand the relevance....
Is the message causing the error an appointment request?
Avatar of jbreg

ASKER

No, the message is triggered when I get an e-mail that satisfies the criteria: it is from xyz@yy.com (and contains correct subject)...
I was thinking about the problem and it may not be the message that satisfies the criteria that's the problem.  It may be any unread message in your Inbox.  The reason for that is this.  When a new item arrives in your mailbox the ItemAdd event is triggered on the Inbox.  Outlook passes the newly arrived item to the ItemAdd procedure.  But what happens if multiple items all arrive at the same time?  Unfortunately, Outlook doesn't call ItemAdd once for each new item, it calls ItemAdd once and passes it one of the newly received items, not all three of them.  The way arround this is to ignore the item passed to ItemAdd and instead grab all unread items in the Inbox and process them.  That's what I'm doing.  So, it could be that there's a problem with an unread item in your Inbox, assuming of course that there are any.  I'll presume that there are, so let's change the following block of code from

    Set olItems = objInboxItems.Restrict("[Unread] = True")
    For Each olItem In olItems
        If olItem.Class = olMail Then
            Set olMailItem = olItem
            'Change the sender's name on the next line to the sender you want to key on
            bolSenderMatch = (olMailItem.SenderName = "eeBlueDevilFan")
            'Change eeTesting on the next line to the subject you want to key on
            bolSubjectMatch = (InStr(1, olMailItem.Subject, "eeTesting", vbTextCompare) > 0)
            'Change the times on the next line to those you want to use
            bolTimeMatch = (Time >= #9:00:00 AM#) And (Time <= #5:00:00 PM#)
            If bolSenderMatch And bolSubjectMatch And bolTimeMatch Then

to

    Set olItems = objInboxItems.Restrict("[Subject] = 'eeTesting'")
    For Each olItem In olItems
        If olItem.Class = olMail Then
            Set olMailItem = olItem
            'Change the sender's name on the next line to the sender you want to key on
            bolSenderMatch = (olMailItem.SenderName = "eeBlueDevilFan")
            'Change the times on the next line to those you want to use
            bolTimeMatch = (Time >= #9:00:00 AM#) And (Time <= #5:00:00 PM#)
            If bolSenderMatch And bolTimeMatch Then

What I've done is grab all the messages with the subject you want to key on rather than all unread messages.  That also allows me to eliminate the subject test later in the code since we already know that the only messages we have are those with the right subject.  Give this a try and let me know what happens.
Avatar of jbreg

ASKER

Same exact error, same exact thing flagged in debug, even with your new code added....
As a test, try commenting out this line

    bolSenderMatch = (olMailItem.SenderName = "eeBlueDevilFan")

then insert this line below it

    msgbox olMailItem.SenderName

This will cause a dialog-box to pop up showing the sender's name.  I'm just curious to see if we can access the SenderName field at all.



Avatar of jbreg

ASKER

Yes, a little popup does come up with the sender's name--but not the sender's e-mail. So it just says "John" rather than john@xyz.com
Avatar of jbreg

ASKER

Interestingly, it does not go away when I click it, and it says the name of senders of other people who have just sent me e-mail. I have clicked it a lot of times but it's still up..is this normal?
Ok, let's try another test.  Uncomment the line you just commented out.  Change the name you're testing against to "John" instead of "john@xyz.com".  Remove the msgbox line too.  Now run the code again and let me know what happens.
No, that's not normal.  The code is somehow still seeing messages it shouldn't see.  Is this line currently in the code?

    Set olItems = objInboxItems.Restrict("[Subject] = 'eeTesting'")

Avatar of jbreg

ASKER

I did what you said using just "John" and commented out Set olItems = objInboxItems.Restrict("[Subject] = 'eeTesting'") and the msgbox line.

Now, I get runtime error 424 object required. Debug points to  For Each olItem In olItems.
No, you can't comment out

     Set olItems = objInboxItems.Restrict("[Subject] = 'eeTesting'")

I was just asking if that line is there.  Uncomment it.  Now change this line

    bolSenderMatch = (olMailItem.SenderName = "john@xyz.com")

to

    bolSenderMatch = (olMailItem.SenderName = "John")
Avatar of jbreg

ASKER

Run time error 4096

One or more items in the folder you synchronized do not match.
Avatar of jbreg

ASKER

Debug points to bolSenderMatch = (olMailItem.SenderName = "John")
Copy and paste this macro into Outlook and then run it.  It's going to pop up a dialog-box showing the number of items that match the filter condition.  In this case it's going to count the number of messages from "John".  If it works and doesn't cause an error, then compare the count it shows to the number of messages in your inbox that are from "John" and let me know if the counts match.

Sub CountRestrictedItems()
    Dim olInbox As Outlook.MAPIFolder, _
        olItems As Outlook.Items, _
        olMessage As Outlook.MailItem
    Set olInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    Set olItems = olInbox.Items.Restrict("[SenderName] = 'John'")
    MsgBox olItems.Count & " items match."
End Sub
Avatar of jbreg

ASKER

Yes it runs without errors and pops up what I believe to be the correct number but since there are almost 2000 it's impossible to count them manually.
Avatar of jbreg

ASKER

Also, is this only going to run when outlook is open? Is it possible (obviously when we get it working) to be a server rule?
> but since there are almost 2000 it's impossible to count them manually
You don't need to count them manually.  Click on the From column and Outlook will group them by sender and show a count for each group.

Yes, this is only going to run when Outlook is open.  Sorry, writing rules to run on Exchange is more complicated and I haven't delved into that.

I'm going to revise the code a little and then I'll post an updated version.  That probably won't be until sometime tonight.
Replace the objInboxItems_ItemsAdd code you have with the code below, then give it a spin and let me know what happens.

Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
    Dim olItems As Items, _
        olItem As Object, _
        olDestinationFolder As Outlook.MAPIFolder, _
        bolTimeMatch As Boolean
    'Change the MAPI folder path on the next line to that of your destination folder
    Set olDestinationFolder = OpenMAPIFolder("\eeBlueDevilFan\eeTesting")
    'Change the Subject and SenderName as needed
    Set olItems = objInboxItems.Restrict("[Subject] = 'Your Subject' And [SenderName] = 'John'")
    For Each olItem In olItems
        If olItem.Class = olMail Then
            'Change the times on the next line to those you want to use
            bolTimeMatch = (Time >= #9:00:00 AM#) And (Time <= #5:00:00 PM#)
            If bolTimeMatch Then
                olItem.Move olDestinationFolder
            End If
        End If
    Next
End Sub
Avatar of jbreg

ASKER

Interesting...

Something just triggered it, it gave Runtime 4096, then moved some (but not all) of the messages that met the criteria and were already in my inbox to the correct folder.

Debug points to  olItem.Move olDestinationFolder.
Well, I'm stumped on what's causing the 4096 errors.  Is there any possibility of changing the rule to work on a different subject and sendername, ones that don't already exist in your inbox, and see if it works okay with them?
Avatar of jbreg

ASKER

Yes, but no it gives the same error....
I'm convinced it's being caused by some setting in Outlook and since the 4096 error points to synchronization and the error is on the line of code that moves an item, I think we have to look in that direction.  So, a few questions:

1.  Are you using Exchange or Internet mail?
2.  Where is your inbox located, online or in a PST file?
3.  Where is the folder located that you want to move the messages to?
4.  Take a look at the properties for both the inbox and the destination folder.  Are there any synchronization settings?  
5.  Are you certain that Cached mode is turned off?
6.  What happens if you drag one of these messages from the inbox to the destination folder?  Does it move properly?
7.  Are there any other rules in affect for these messages?
Avatar of jbreg

ASKER

1. Exchange. Also be aware that this is used with BlackBerry Enterprise Server for Exchange
2. Online
3. Online--on the exchange machine (but as is cached exchange mode there is a local copy I would imagine)
4. Inbox
Statistcs
Last Synched on Today (time)
Server folder contains x items
Offline folder contains  x items

Folder in question:

Last synched: a week ago
Server folder contains 442 items
Offline contains: 0 items
5. Yes 100%. Box is cleared in mailbox properties. However, it doesn't really matter as it's not a good solution if I can't have this turned on.
6. Works perfectly.
7. No.
I wonder if what we're seeing has anything to do with the Blackberry integration?  I don't have a Blackberry server so I've no way to test that.
Avatar of jbreg

ASKER

Not blackberry per se, but the fact that messages are synchronised is probably where the problem lies.
Since I don't have access to a Blackberry server I've no way of testing this or attempting to engineer a solution.  I'd propose a test where you turn off Blackberry support on your mailbox to see if that solves the problem, but if Blackberry support is important to your work, then that wouldn't do.  How do you want to proceed?
Avatar of jbreg

ASKER

ABSOLUTELY NOT! Don't you read the answers and replies?

The solution provided by the expert DOES NOT FUNCTION AT ALL. I have indicated this. I was waiting for a response from another expert which is why the question was left open.

If you accept this answer you will lead people to believe, wrongly, that his solution will work for them. Sorry, Bluedevil, nothing personal you did great, but I just can't stand the fact that people seem to want to force acceptance on solutions which aren't really solutions! This demeans EVERYTHING we are trying to do in these forums.

PAQ with Points Refunded. You know how strongly I feel.
jbreg,

I don't take your position personally although I don't agree with it.  However, I do object to your statements "The solution provided by the expert DOES NOT FUNCTION AT ALL", since it's untrue, and "If you accept this answer you will lead people to believe, wrongly, that his solution will work for them.", since you cannot possibly know if this solution will work in another environment or not.  I tested what I posted before posting it, and it does work exactly in my environment (actually in two since I tested it on my personal system and on my office system).  So it does work, and it might well work for others too, since all indications are that the problem is confined to your environment.  I realize you probably meant that the solution does not function at all for you, which I would agree with.  But the way it's stated would lead a reader to believe that the function doesn't work at all, and that's just not true.  Just because something does not work in one place does not mean it doesn't work.

I did everything I could to troubleshoot the problems we ran into, but I cannot duplicate your environment and was not able to figure out what's causing the synchronization error.  I did propose an additional test and asked how you wanted to proceed, but you never replied.  That was over three months ago.  No other expert ever participated in the question and since I see that you've asked nearly 60 questions you must know that questions here are like missing persons: the longer they are missing the less likely they are of being found.  The longer a question goes unanswered, the less the liklihood of a new expert joining in and providing a solution.   If no one has joined the question after three months it seems pretty unlikely that they ever will.  EE's help on this issue (https://www.experts-exchange.com/help.jsp#hi5) states something very similar.  I also took a look at your profile and counted approximately 13 questions that have either been classified as abandoned or will be if they aren't closed.  One of those questions is coming up on six months.  All of this diminishes your statement that "I was waiting for a response from another expert which is why the question was left open."  EE's guidelines on what happens if you don't close your questions (https://www.experts-exchange.com/help.jsp#hi78) states, "It is also up to the Author of the question to reply to all experts comments ...", which you did not do.  I don't usually object to the question author seeking a refund if the solution doesn't work, so I would have supported your request in that area if you had made it in a timely fashion.  But I don't agree with a refund request that comes more than 90 days after the fact and when the author stopped responding.  I think a refund is inappropriate in any case where the author abandons their question.  Beyond that, I am not concerned about whether I get the points or not.
Avatar of jbreg

ASKER

First, I'll reply to your point just to get things moving, then I'll respond to the subtance of your points. I first want to say that you clearly deserve more than 500 points for your efforts--you really went the extra mile to help. But, at the end of the day, I don't see the potential for a solution here to the question I asked, which naturally entails working in my environment:

Since I don't have access to a Blackberry server I've no way of testing this or attempting to engineer a solution.  I'd propose a test where you turn off Blackberry support on your mailbox to see if that solves the problem, but if Blackberry support is important to your work, then that wouldn't do.  How do you want to proceed?

This is great, but it won't be any use to me at all. I could hypothetically go through all this work to do it, and it might work--I am quite certain it will--but that won't help me because I can't just x BES out of the equation as we use BES here and I can't just make a change that severe because of this, as I am sure you understand.

What do you think of this--let's take your word that this works in non-bes / sync environments and I'll close and award you the points, then open ANOTHER question which basically references this one and says we need to get it to work in an environment with synchronisation / BES, hoping that someone else will be able to tackle that one?

All I really want is an answer to the question, I have had questions go dormant (not because I haven't replied to comments, but because experts haven't replied to mine--viz the questions you referenced) and then get answered by people surfing in much later. By closing the question, we forever lose the knowledge that might have been gained in this manner.

What do you think?
A very well written response.  I appreciate your position and I'll start off by saying that I'm not seeking to get the points.  My only purpose in discussing this issue further is the exchange of thoughts.  I'd also like to say thanks for discussing this and not turning it into a series of flames.  It's nice to be able to disagree without the exchange turning disagreeable.

>... at the end of the day, I don't see the potential for a solution here to the question I asked, which naturally entails working in my environment:

I understand, but had no way knowing that your environment include a Blackberry server.  Had your question contained that piece of information, then I would never have offered a solution or would have offered it as a "this may or may not work" with an upfront understanding that I'm taking my chances.  It seems a little unfair to penalize the expert when in the process of troubleshooting a factor comes into play that was never mentioned in the original question.  I know it wouldn't be practicle to list everything in a given environment in each question, but at a minimum I think question authors ought to include the OS version, the software version, and mention any other pieces of software that might clearly affect answering the question.  I wish EE would redesign their question page to require that information, but that's not something we can solve.


> I can't just x BES out of the equation

I understand, and mentioned in my 3/29/2005 post, that turning off the Blackberry server wouldn't be an acceptable solution if it is integral to your work, which it obviously is.  But as a test it would have told us a lot.  Had we performed that test and the problem persisted, then we would have known it wasn't the Blackberry server and could have proceeded with other troubleshooting steps that might have led to a solution.  And had the problem gone away, then we would have known that it was the Blackberry server and could have searched for known issues when a Blackberry server is involved.  At the very least you would have known the cause.  Armed with that information you could have opened another question about Blackberry/Outlook integration problems, or taken other steps to get an answer.  As it stands we don't know for sure what the problem is.

> What do you think of this--let's take your word that this works in non-bes / sync environments and I'll close and award you the points, then open ANOTHER question which basically references this one and says we need to get it to work in an environment with synchronisation / BES, hoping that someone else will be able to tackle that one?

If we had established that the problem is with BES, then yes, that does seem the fair way to handle it.  The question asked for a macro that would perform a particular action.  If my code performs that action but won't work in your environment because of BES, which was not part of the original question, then doesn't a separate question about why BES interferes with Outlook code execution seem appropriate?


> All I really want is an answer to the question

And all I wanted to do was help get you that answer.  But I can't do that if we can't run the tests necessary to isolate and solve the problem.
Avatar of jbreg

ASKER

Let's assume you are right, then what?

1) Your solution works in Exchange 2003 implemenations without BES.
2) Your solution does not work in my environment.
3) Therefore, I can't implement your solution even if it does work.

I could spend hours either decoupling a mailbox from BES or creating a new one but then what? What would I prove, at the most? What you have already proven! That your solution works in an exchange 2003 implemenation without BES. I don't need to do this because I trust in you that you have already tried this. Why waste any time on it? I agree it probably works. I am almost certain at this point that it does. What would be the result if the test were positive? It might answer THE question but not MY question--so what's the point? I believe it works on your end, I really do.

If you belive me like I believe you, you will see that sync is at the heart of the problem. Why? What's the difference between your environment and mine? How will that impact your answer? What changes will you make as a result. Believe me like I believe you.

The only reason for you to encourage me to re-create this here is some uncertainty that it will work. That's not at issue, at least not on my part. So it's moot. These questions ALWAYS assume that the answers will work in the environment in which the questions are asked--or else this is all just academic. This site is valuable because people are able to obtain answers which function in the "real world" not answers of value only on some obscure knowledge base. If the answers provided by experts work on configurations other than the context in which the questions are asked, that's great, but it doesn't much help those who ask the questions in the first place.

Do you doubt that the solution does not work in a non-BES environment? What environment are you testing? What might impact the answer? Where is the rub? Where is the solution? Where is the understanding? Where is the difference?
We could debate this to death but I don't think we're going to agree.  We've both made our positions clear so the decision on how to resolve this is in the hands of EE.  
PAQed with points refunded (500)

modulo
Community Support Moderator