Catch specific emails and respond

Joined
Mar 15, 2011
Messages
19
Reaction score
0
Hi peeps, im kinda new to VBA, but i'm quite grounded in VB6 and i need to make a macro for my outlook 2k7

The idea is i want to trigger the event for every new unread email, check the subject and the address its sent from, and when that corresponds i need to pull info out of it, store it in an MDB (which i have a little experience with) and then create a response, but not responding to the sender instead responding to an email address i harvest from the email itself.

The problem is i dont know the events and syntax of outlook VBA hardly at all.

can anyone give me a few pointers please, even some links to nice noob tutorials would help.

Thanks in advance

Molly
 
Hi!

I have recently been doing something pretty much along these lines; I'm posting what I have that I believe is relevant for you. All the code is in the ThisOutlookSession in the VBA editor.

-------- 8< --------
Public WithEvents objInbox As Outlook.Items

Private Sub Application_Startup()
Set objInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Sub DoStuff(ByRef Item As Outlook.MailItem)
MsgBox "Subject: " & Item.Subject & ", sender: " & Item.SenderEmailAddress
End Sub

Private Sub objInbox_ItemAdd(ByVal Item As Object)
If Item.Class = olMail Then
DoStuff Item
End If
End Sub
-------- 8< --------

Two links regarding what I do above:

http://msdn.microsoft.com/en-us/library/aa171270(v=office.11).aspx
http://msdn.microsoft.com/en-us/library/aa210946(v=office.11).aspx

Hopefully this will get you started!

///BR, Jens Carlberg
 
thanks. i tried it but it doesnt seem to be firing when i get a new mail. i tested it by inserting msgboxes at each point, but DoStuff never triggers.

what am i doing wrong? am i missing a reference library?
 
I'm using Outlook 2003. If you are using another version, I image there might be differences.

Also, I often have to restart Outlook to get my macros to work; when starting Outlook, I get a question if I want to enable macros.

Other than that, I'm out of ideas.

///BR, Jens C
 
pants. im using outlook 2k7, and i think ive got it sorted now.

Code:
Option Explicit

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
  Dim objItem As MailItem
  Set objItem = Application.Session.GetItemFromID(EntryIDCollection)

  If fTestIncoming(objItem) = True Then sProcessIncoming objItem
End Sub

Function fTestIncoming(objNewMail As MailItem) As Boolean
  Dim blnIsRelevant As Boolean
  blnIsRelevant = False
  If LCase(objNewMail.SenderEmailAddress) = "[email protected]" And LCase(objNewMail.Subject) = "enlarge your pens" Then
    blnIsRelevant = True
  End If
  fTestIncoming = blnIsRelevant

End Function

Sub sProcessIncoming(objNewMail As MailItem)
'stuff happens
End Sub
now all i need is to get the STUFF to HAPPEN :D

any idea how i connect an mdb to outlook? i need to push/pull tables in an external mdb now and i have NO idea how i assign an external database to the Database variable
 
Back
Top