Solved

Using VBA in Outlook to automate email responses.

Posted on 2003-10-29
8
773 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

14 Experts available now in Live!

Get 1:1 Help Now