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

bare minimum setup for automatic macro

Status
Not open for further replies.

Sniipe

Programmer
Oct 9, 2006
115
IE
I'm using outlook and alf + F11 brings up the VBA

In their I have access to ThisOutlookSession. I wanted to popup a msgbox("hello world") when I recieved a new mail.

I assumed this went into:
Code:
Private Sub Application_NewMail()
    MsgBox ("hello world")
End Sub

What do I need to do to get that popup evertime I get a new mail? I'd like to be able to close outlook and for it to save (its only going to be on my local machine)

I think I've to compile it and save it to be a saved macro (how?) plus I've to get it to start every time a new outlook opens.
 



Hi,

In Outlook VB Help, search on Events.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
sorry SkipVought, I don't get you. when I go to my outlook 2010 and I click alt+f11 and then I click help I get the bing search, which has nothing... absolutely nothing at all in it.
 
Then you must not have all the features loaded that you need.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I got round my problem and created the code I needed which fires off when I receive a new mail. The problem is now that the function Application_NewMail() does not catch all new mail.
eg if I open outlook I might have 3 emails waiting. I want to alter the subject in these as I do in Application_NewMail() but I don't know what to do.

I think I'll have to have some get function to get all new mail?
Then I will have to loop through each of them and call the same code in Application_NewMail()?
 
this is what I have so far:
Code:
Private Sub Application_Startup()
    Dim olApp As Outlook.Application
   Dim olNS As Outlook.NameSpace
   Dim olFld As Outlook.MAPIFolder
   Dim oMail As Outlook.MailItem
   
   Set olApp = Outlook.Application
   Set olNS = olApp.GetNamespace("MAPI")
   Set olFld = olNS.GetDefaultFolder(olFolderInbox)
   
   olFld.Items.Sort "Received", False
   
   Set oMail = olFld.Items.GetFirst
   
   If oMail.UnRead Then
        Do While oMail.UnRead
            Call AlterSubject(oMail)
            Set oMail = olFld.Items.GetNext
        Loop
   End If
End Sub

Private Sub AlterSubject(ByVal o_mail As Outlook.MailItem)
   Dim DateValue As String
   Dim TimeValue As String

   DateValue = Format(o_mail.SentOn, "yymmdd")
   TimeValue = Format(o_mail.SentOn, "HH:MM")

   o_mail.Subject = DateValue + " " + o_mail.Subject + " (" + TimeValue + ")"
   o_mail.Save
End Sub

But what happens is that it is stuck on an infinite loop...
I think its to do with Set oMail = olFld.Items.GetNext in the loop. I expect it to move to the next mail item but it doesn't
 


Put a BREAK in your loop and examine the property that you are testing.

faq707-4594

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top