Solved

Using VBA in Outlook to automate email responses.

Posted on 2003-10-29
8
776 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:ajet7
  • 4
8 Comments
 
LVL 5

Accepted Solution

by:
jrocnuck earned 250 total points
ID: 9645936
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
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9645944
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
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9648070
why do you need to use a vba ? this can be done by rules itself.
0
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9650723
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
 
LVL 5

Expert Comment

by:jrocnuck
ID: 9650834
sorry.. in my last post, I meant moving the folder item to another location..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now