Using VBA in Outlook to automate email responses.

Hello,
I have written some VBA that is triggered when an email comes into the Inbox (outlook 2000).  The VBA connects to a DB, finds some information, then responds to the email with that information.  That email is then moved to a different folder in Outlook. Then, I have query that checks the Inbox to see if there are more emails.  if there is one, a variable grabs the top record (the first email in the Inbox), and assigns it to an Object variable.  

The problem:

olInboxItems_ItemAdd(ByVal Item As Object)

seems to not want to let me call it again and pass the next email to it.  I am trying to figure out how to get the VBA to cycle through the emails of there are more in the inbox.  

This is where I query the inbox, assign the next email to the variable, and pass it through... all i keep getting is "Object required" Error no matter what i try:
(Dims)
objNS2 is a Namespace
myFolder is a MAPIFolder
tempItem is an Object

----****other code****----
Set objNS2 = Application.GetNamespace("MAPI")
Set myFolder = objNS2.GetDefaultFolder(olFolderInbox)

'assign the next email to tempItem
Set tempItem = myFolder.Items(1)

'pass tempItem to the original function to start the process again with a new email
olInboxItems_ItemAdd (tempItem)
----****other code****----

I AM DESPERATE!!!!!!  Can you help?

Thank you SOOOOOOOOOOO much for your help!

Eric

PS... the few times it accepted something, it simply used the original email and never picked up the new one... then after two more lines, it crashed. hope this doesnt confuse!
ajet7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jrocnuckCommented:
could it be that the function is getting called because a mail item comes in during the process, so then you have called the macro 2 times and have the macro acting twice on the same mail item?

maybe you should have a global variable that prevents re-entry into the macro, or have some form of semaphore that blocks acesses to the mail item until all moving and processing has completed.

in otherwords, once you call the macro, you should do the following procedure:

1. Disable new mail triggering of the macro (or enable blocking of new calls to the macro i.e. no re-entry)
2. Process (do-while loop) inbox mail items until myFolder.Count is equal to zero
3. When finished with all inbox items, re-enable the triggering of the macro or rentry of the macro
4. exit macro
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jrocnuckCommented:
you could try it with a vbs script as well..

I used this to display an email .. basically created a .vbs file on my desktop and double clicked it.. you'd have to supply the rest of the code for your olInboxItems_ItemAdd(ByVal Item As Object) if you were to try it with the vbs script.

Dim objNS2
Dim myFolder
Dim tempItem
Dim app


Const olFolderInbox = 6

Set app = WScript.CreateObject("Outlook.Application")
Set objNS2 = app.GetNameSpace("MAPI")
Set myFolder = objNS2.GetDefaultFolder(olFolderInbox)
Set tempItems = myFolder.Items(1)
'olInboxItems_ItemAdd (tempItem)

' just open the item..
tempItems.Display
0
bhagyeshtCommented:
why do you need to use a vba ? this can be done by rules itself.
0
jrocnuckCommented:
I gathered from the first post that ajet7 is parsing some info from the email and using that as a query to a database and then based on that information, doing something else, followed by moving the folder to another location.  If the database is being used to determine where the item should be moved, then mabye rules would work well.
0
jrocnuckCommented:
sorry.. in my last post, I meant moving the folder item to another location..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.