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

Outlook -> Receive Mail Notification 2

Status
Not open for further replies.

dcurtis

Technical User
May 1, 2003
273
US
(Originally Posted in Microsoft: Office w/no answers)

Outlood 2002 on XP machines.

I can make the message box appear when I get new mail (you have new mail, do you want to check it now (yes, no)). Is there a way to customize that box to display who it's from? So it would say, you have new mail from ....... Do you want to read it now?


----
A program is a spell cast over a computer, turning input into error messages.
 
Use the VB Editor to navigate to Project1(or whatever yours is called)->Microsoft Outlook Objects->ThisOutlookSession. Then select Application from the left-hand combo box (above where you type the source code) and select NewMail from the right-hand combo box. This should give you the following event structure:
Code:
Private Sub Application_NewMail()

End Sub

Next, add the following code to this event:
Code:
  Dim objItem As Outlook.MailItem
  
  Set objItem = Application.GetNamespace("MAPI"). _
   GetDefaultFolder(olFolderInbox).Items(1)
  MsgBox "New Mail" & "Subject: " & objItem.Subject & vbCrLf & _
         "Sender: " & objItem.SenderName

This has the effect of showing a message box containing the Subject and SenderName of the first email in the folder, every time an email is received. So the assumption is that the first mail item in the folder contains the most recently received email. You may need to turn off the notification message option that would normally appear.


Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
An alternative would be to insert a UserForm using the VB Editor, add a label and two buttons to it (with captions "Yes" and "No") and replace the above code with the following:
Code:
  Dim objItem As Outlook.MailItem
  
  ' Get the item that likely triggered the event.
  Set objItem = Application.GetNamespace("MAPI"). _
   GetDefaultFolder(olFolderInbox).Items(1)
  UserForm1.Label1.Caption = "Subject: " & objItem.Subject & ", " & _
                             "Sender: " & objItem.SenderName
  UserForm1.Show

In the Click event of the "Yes" button of the form put this code:
Code:
  Dim objItem As Outlook.MailItem
  
  Me.Hide
  ' Get the item that likely triggered the event.
  Set objItem = Application.GetNamespace("MAPI"). _
   GetDefaultFolder(olFolderInbox).Items(1)
  objItem.Display

In the Click event of the "No" button of the form put this code:
Code:
  Me.Hide

Hope you find this useful!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Works great, w/2 problems. First is I get a warning every time about a program trying to access my address book, could be virus.......do you want to allow. I can select yes, it works fine. I'm checking settings on that now.

Second, I sort my mail by received, newest on top. The code selects the bottom (oldest) item in my inbox.

----
A program is a spell cast over a computer, turning input into error messages.
 
Oh well, here's a star for the code that worked. Administrator doesn't want to allow code to run automatically. I guess if I were in his shoes, I wouldn't either.

Thanks again

----
A program is a spell cast over a computer, turning input into error messages.
 
If you ever need to try this... try this it works!!

Private Sub Application_NewMail()


''This has the effect of showing a message box containing the Subject and
''SenderName of the first email in the folder, every time an email is
''received. So the assumption is that the first mail item in the folder
''contains the most recently received email. You may need to turn off the
''notification message option that would normally appear.


Dim objItem As Outlook.MailItem
Dim cnt

cnt = Application.GetNamespace("MAPI"). _
GetDefaultFolder(olFolderInbox).Items.Count

Set objItem = Application.GetNamespace("MAPI"). _
GetDefaultFolder(olFolderInbox).Items(cnt)
MsgBox "New Mail" & "Subject: " & objItem.Subject & vbCrLf & _
"Sender: " & objItem.SenderName

End Sub

''This will post the current message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top