Solved

Using VBA in Outlook to automate email responses.

Posted on 2003-10-29
8
784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

732 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