Set objInspectors = Application.InspectorsĪnyway, the NewInspector event is fired whenever an Outlook item is opened: Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector) We also need to work with two class events to set these objects and clean up when the class is disposed of in the ThisOutlookSession.Application_Quit event: Private Sub Class_Initialize() When I declare this object WithEvents, it exposes the one and only event you can work with in that collection – the NewInspector event. This is another intrinsic object collection exposed by the Outlook Application object. The first one in the chain is the Inspectors collection. To do that, I need to declare three objects within the class to setup the proper hooks: Option Explicitĭim WithEvents objInspectors As Outlook.Inspectorsĭim WithEvents objOpenInspector As Outlook.Inspectorĭim WithEvents objMailItem As Outlook.MailItem This class is meant to get programmatic access to any e-mail message that is created or opened.
![visual basic for applications outlook visual basic for applications outlook](https://imag.malavida.com/mvimgbig/download-fs/visual-studio-2010-6538-2.jpg)
Now we come to the meat of the solution – the clsMailItemTrapper class. I also make sure to set this class to Nothing when Outlook's Application_Quit event fires so that I can release the memory I've claimed with any object references that are being used. The Startup event ensures that any events I've declared in that class which is tied to a particular Outlook item is trapped so that I can perform operations on that item with my code.
Visual basic for applications outlook code#
In the above example, I am instantiating a variable declared in the module to load my code that is stored in a custom clsMailItemTrapper class that I've added to the VBA Project (make sure to choose Insert|Class Module in the IDE to create and name this class correctly this is where all of the code below needs to be stored). Every other Outlook object is derived from the Application object. The Startup procedure is an event exposed by the intrinsic Outlook Application object – the big daddy object for Outlook development. Set myMailItemTrapper = New clsMailItemTrapper
![visual basic for applications outlook visual basic for applications outlook](https://dolphy35.developpez.com/article/outlook/vba/images/select_commandes.png)
The most important thing that needs to be added is the Startup event: Option Explicitĭim myMailItemTrapper As clsMailItemTrapper The ThisOutlookSession module needs only a handful of code to be written that'll ensure your code runs at the right time. This special module, along with other modules, classes, and custom user forms that you can create are listed in the Project Explorer in the IDE (CTRL+R, or View|Project Explorer). You can open this with ALT+F11 or Tools|Macro|Visual Basic Editor. This always lives within the Outlook Visual Basic Editor or IDE (Integrated Development Environment). The key to this is the ThisOutlookSession module. There is also a comprehensive summary of Outlook programming at the Developer Learning Center for Microsoft Outlook: …) (Before I go any further, I just want to briefly cover the basics of working with macros in Outlook in case you are new to this. The secret is to use a mixture of events, collections and item objects that get created and properly bound every time Outlook starts, so that your code will always run. Set objMailItem = īut what if you ALWAYS want this action to occur? It is not very productive to manually click a custom toolbar button to fire you macro that will run your code every time you need access to an item.
![visual basic for applications outlook visual basic for applications outlook](https://static.slipstick.com/images/2011/07/Trust-center1.png)
Is there something like GetMessage that will return the open e-mail? Not really, but close - you can easily write code in an ad-hoc macro to get this item and work with it like this: Dim objMailItem As Outlook.MailItem
![visual basic for applications outlook visual basic for applications outlook](https://cdn.extendoffice.com/images/stories/doc-excel/open-specific-folder/doc-open-outlook-folder-1.png)
Let's say you want to perform some custom actions in code with an e-mail message.