Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA outlook receive mail event 1

Status
Not open for further replies.

dgillz

Instructor
Mar 2, 2001
10,045
US
I am trying to launch code when a new mail item is received in outlook and am struggling. There is not much documentation that I can find but here is what I have so far:

Code:
Private Sub Application_ItemReceive(ByVal Item As Object, Cancel As Boolean)
        MsgBox "You have Mail!"
End Sub

I plan on replacing "You have mail!" with some real code, so please don't tell me I don't need VBA to do this. I do need VBA to do what I want to do, but I need the code to fire when I receive new mail. Right now nothing happens.

Any help appreciated.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Maybe you're looking at the wrong event.
I'd go with:
Code:
Private Sub Application_NewMail()

End Sub

Don't count the days, Make the days count.
 
Chris,

So this event will fire when I receive mail?

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
OK quick followup question. How do I test for a specific incoming email address and subject line?

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Set ref to :
.GetDefaultFolder(olFolderInbox).Items.GetFirst

Don't count the days, Make the days count.
 
Set ref? Can you tell me what that means? You don't mean references under tools-> references do you?



Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports

"What version of URGENT!!! are you using?
 
Try
Code:
Private Sub Application_NewMail()
    Dim oNS         As NameSpace
    Dim oFolder     As MAPIFolder
    Dim oItem       As Object
    Dim oNewMail    As MailItem
    
    Set oNS = GetNamespace("MAPI")
    Set oFolder = onMAPI.GetDefaultFolder(olFolderInbox)
    Set oNewMail = ofFolder.Items.GetLast
    
    With oNewMail
        MsgBox .Subject & " " & .SenderName
        
    End With

End Sub

Don't count the days, Make the days count.
 
or maybe
Code:
Private Sub Application_NewMail()
    Dim oNS         As NameSpace
    Dim oFolder     As MAPIFolder
    Dim oNewMail    As MailItem
    
    Set oNS = GetNamespace("MAPI")
    Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
    Set oNewMail = oFolder.Items.GetFirst
    
    With oNewMail
        MsgBox .Subject & " " & .SenderName
        
    End With

End Sub

Don't count the days, Make the days count.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top