Outlook Automation Example

You want to find out how to write VB/VBA code to create and send a message using Outlook. You don't know anything about the Outlook Automation interface.

Start Outlook, then start the Inspector. Expand the "Outlook" tree node on the left panel, which should look similar to this:



Click on the "Show Documentation" link next to "Application" at the bottom, and you will get this:



Ok, you see a "Create Item" on the documentation window, you also see a "CreateItem" method that's part of the Application object. This looks promising, let's try it. Click on the "CreateItem" method, and the parameter panel will look like this:

Things are looking better as the first item in the ItemType is an "olMailItem", which is probably when we want. So just press "Invoke Method". This calls the CreateItem method on the Application object which returns a MailItem, as shown in the value portion of the tree. Expand the CreateItem method (to view this object).



At this point, Outlook pops up a dialog indicating you are using the Automation interface to access a MailItem:

Say yes, and give yourself 10 minutes.



Let's explore what this MailItem thing is. Open up the properties and you have:



Click MailItem's Show Documentation link, giving you:



A mail message; this is clearly what you want. Click on the "To" property and set it to where you want to send to, by filling in the value and pressing the "Set Prop" button:



Ok, where do we put the text? The documentation is not very helpful, there is no example to construct the message. Lets just look through the properties to see if one will work. The Body property looks good (after all, you can see that its a string type), lets just confirm by checking its documentation. Click on the Body property, fill in the value, and click the "Set Prop" button.

Now, lets send the message. Open up the Methods node (at the same level) as the Properties node. We see a Send method. Invoke it.



Again, Outlook asks if we should do this:



And we see the message is sent.