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

Display MsgBox when new email arrives

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I've just installed Visual Studio 2008 on my PC and I'm attempting to write an Outlook addin. I have a good understanding of vbscript and have adapted some VBA examples before but there are some fundamental questions which are preventing me from moving forwards.

My code at the moment is:

Code:
Public Class ThisAddIn

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    End Sub

    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    'My new code to go here?
    'End of my code

End Class

If I want to check my inbox for new emails arriving does the code to do that go where I've put my comments or am I in the wrong place? Sorry if that is a basic question most of the tutorials assume that the user has more than zero knowledge!

Next - I've been looking at an example here:


The first line is:

Code:
Public WithEvents myOlApp As Outlook.Application

What does this line actually mean? Is it declaring an object myOlApp? If so what does WithEvents get added for?

Next we have

Code:
Sub Initialize_handler()
    Set myOlApp = CreateObject("Outlook.Application")
End Sub

This part I understand!

Then we have

Code:
Private Sub myOlApp_NewMail()
End Sub

This is going to sound a very stupid question but if you call a sub xxx_NewMail() does that mean run the code in that sub when a new mail arrives or can you call it anything you want and the code that checks for new mail is contained within the sub?

Thanks - I think if I've got these basics right then I'll be at least able to move forward in small steps!

Ed
 
Basically, WithEvents is used to specify that the object variable will be used to respond to events triggered by the object.

As you've used WithEvents, myOlApp_NewMail() will be called when a new mailitem arrives (you'll need to keep it named the way it is), in the same way that Application_NewMail sub will be called when a mailitem arrives if you're coding from within Outlook. You put your code in to tell Outlook what you want to happen when this event fires.

There are a couple of links in the article you posted that might help here and here.

Hope this helps

Andy
---------------------------------
Zebracorn: 50% Zebra, 50% Unicorn = 100% Real.

 
Hi Andy,

Thanks for this that's very helpful. I have now modified myOlApp_NewMail() so it reads:

Code:
Private Sub myOlApp_NewMail()
  MsgBox("Hello World")
End Sub

The aim is for a popup to appear saying "Hello World" whenever a new message arrives. At the moment nothing is happening - do I need to tell it to actively monitor my Inbox?

My overall code now reads:

Code:
Public Class ThisAddIn
    Public WithEvents myOlApp As Outlook.Application
    Sub Initialize_handler()
        myOlApp = CreateObject("Outlook.Application")
    End Sub
    Private Sub myOlApp_NewMail()
        MsgBox("Hello World")
    End Sub
End Class

Thanks

Ed
 
The example you have been looking at is for VBA. However your code is not VBscript, not VBA, nor even VBA. It is VB.NET, which is a completely different kettle of fish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top