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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

urgent:How to catch as object when a "Newmail" arrives in outlook?

Status
Not open for further replies.

VB1VBA

Programmer
Oct 2, 2002
5
US
Hi,

I want to catch up the new mail message as OBJECT when it arrives to the outlook folder. The folder needn't be "inbox" only it can be any user defined folder under the outlook mailbox.

Can anyone help me out in this?

hope the problem is clear.

it's very urgent.
 
I have done something similar before. The program I wrote scanned through the Inbox for unread messages. Any unread messages were processed then marked as read so they were ignored by the next scan.


My code looked like this:

Dim App As Outlook.Application
Dim NS As Outlook.NameSpace
Dim Fldr As Outlook.MAPIFolder
Dim Mail As Outlook.MailItem
Dim i As Integer

On Error Resume Next

Set App = New Outlook.Application
Set NS = App.GetNamespace("MAPI")

Set Fldr = NS.GetDefaultFolder(olFolderInbox)

For i = 1 To Fldr.Items.Count
Set Mail = Fldr.Items(i)

'There will be no error if it is a MailItem
'An error occurs if it is another type of item
'like a MeetingItem

If Err = 0 Then
If Mail.UnRead Then
'****Process the MailItem here****

Mail.UnRead = False
End If
Else
Err.Clear
End If
Next i



Something like that. The NameSpace object has a folders collection so you could loop through all the folders.

Hope this helps.




 
Hi ITMannie,

Thanks for your code. It works fine. I have one more problem....

I wanted to catch up sender's name from the new mail arrives. So I used the logic as follows.

Public Sub Application_NewMail()
Dim flag As String
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)

Set myitem = myFolder.Items(1)

StrSenderName = myitem.SenderName
msgbox StrSenderName

End Sub

This logic will work only for the inbox folder and pick up the first mail in that without verifying it is new mail or not.

But I wanted to have a general logic for the new mail even if it directed to any other folder than Inbox.


Hope I explained this problem at my best.

waiting for your kind reply.

thanks,



 
I checked out the events that are available to check for new mail (I never noticed them before because I use Outlook 98 at work which doesn't have these events - the events are only in Outlook 2000 onwards)

Anyway, the NewMail event is an Application event that only works for the Inbox but there is another event you can use. You want to use the ItemAdd event of the Items class.

The example below has been lifted straight out of the Microsoft Outlook VBA help file (if you search for ItemAdd you will get ItemAdded which is not the same - you have to navigate through the following items in the menu - Microsoft Outlook Visual Basic Reference --> Events --> I --> ItemAdd Event)

Anyway, here is Microsoft's sample code:

'============================
ItemAdd Event Example

In this example, when a new contact is added to the Contacts folder, the contact item is attached to a mail message and sent to a distribution list named Sales Team. The sample code must be placed in a class module, and the Initialize_handler routine must be called before the event procedure can be called by Microsoft Outlook.

Dim myOlApp As Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Set myOlItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)
myOlMItem.Save
Set myOlAtts = myOlMItem.Attachments
' Add new contact to attachments in mail message
myOlAtts.Add Item, olByValue
myOlMItem.To = "Sales Team"
myOlMItem.Subject = "New contact"
myOlMItem.Send
End Sub
'================================

This should allow you to do what you need to do. In the Initialize_handler just set the items to the folder you want to check. I assume this will work - I haven't checked it.

Hope this helps.






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top