WithEvents - Application Level Code?

D

Don Miller

I was trying to use VBA at an application level and I am unable to even get
these simple scripts to run on the events below when a mailitem is read or
opened (and what's the difference between them?).

Do you have to restart Outlook or the computer between code changes?

Thanks for any tips.

Dim WithEvents myMailItem As MailItem

Private Sub Application_Startup()

End Sub

Private Sub myMailItem_Open(Item)
MsgBox "open"
End Sub

Private Sub myMailItem_Read()
MsgBox "read"
End Sub
 
S

Sue Mosher [MVP-Outlook]

You're not instantiating the myMailItem object. In other words, Outlook
doesn't fire any event, because you haven't told it *which* MailItem you
want to work with. FWIW, this is not simple stuff: If you really want to
work with all MailItem objects, you need a wrapper class to handle multiple
open and selected items.
 
D

Don Miller

I did try (I thought) in several dozen attempts to instantiate ALL
myMailItems (that were in the Inbox) with something like that below, but I
didn't have anymore luck with that. Could you please point me to somewhere
that describes what a wrapper class looks like and how it is implemented in
VBA? I don't understand why the Read() function doesn't work since I only
display a msg!

Thanks (I'm functionally competent in VB but new to VBA and have never
needed wrapper classes).

Option Explicit
Dim WithEvents myMailItems As Items
Dim objNS As Outlook.NameSpace

Private Sub Application_Startup()
Dim objInbox As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set myMailItems = objInbox.Items
End Sub

Private Sub Application_Quit()
Set myMailItems = Nothing
Set objNS = Nothing
End Sub

Private Sub myMailItems_Read()
MsgBox "read"
End Sub
 
S

Sue Mosher [MVP-Outlook]

Your code below instantiantes an Items collection, e.g. all the items in a
folder. Items, however, does not have a Read event.

The classic example of a wrapper class is found in the Items Command Bar
sample from http://www.microeye.com, which includes a wrapper for Explorer
objects. Building one for MailItem objects is more complicated, because you
have to deal with both selection changes in a folder and new items opening
in the currently open Inspector object (= the window that Outlook displays
items). Relevant events are Explorer.SelectionChange,
Inspectors.NewInspector and possibly Inspector.Activate.

Maybe this is a good time to ask what your actual application goals are.
Perhaps there is a simpler route.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
D

Don Miller

Maybe this is a good time to ask what your actual application goals are.
Perhaps there is a simpler route.

I think maybe you're right. Forget the VBA and start a new habit by dragging
inbox items to a mail archive folder when I want to save them.
 
S

Sue Mosher [MVP-Outlook]

Or if you already have the message open, instead of closing it, choose File
| Move to Folder.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top