Within Outlook, you can enhance its functionality by creating macros to automate tasks that you find yourself doing repeatedly. We will use the Visual Basic for Application (VBA) to learn how to apply programming features to Outlook to automate repetitive tasks and create macros.
Let’s discuss the following topics:
Through these topics, we will focus on the basics of programming within VBA to create macros within Outlook for beginners. This chapter will get you started with VBA and you will find several use cases, such as creating other code, for using VBA.
A data model is a blueprint that shows the hierarchical abstract model that organizes Outlook data in a particular order. Outlook, as well as other applications, has its own data model and it is called the Outlook object model. I have noticed in some documentation written on the Outlook object model that classes are referred to. Some of the classes/objects that Microsoft identifies as high-level or important objects are the following:
Microsoft defines a class/object as a description or template that is used to create or instantiate an object.
You can read more about the Outlook object model in the Further reading section of this chapter. The following table of terms will help further explain the object model and it will be useful for you to know these terms if writing VBA code:
An example of using an object would be the namespace object. It is the root of the object model. The only item higher than the namespace would be the actual Outlook application. Within the namespace object, we can also have all kinds of folders, contacts, mail, people, tasks, and more. Depending on which folder you have selected will depend on what actions can be taken. For example, if I am in my mail folder (Inbox), I have access to my emails but not my Calendar information. Becoming familiar with the object model will help you be able to refer to names more easily. At first, you may find it confusing, but the more you use it and apply the features to coding within VBA, the easier and more robust you will find it to be. After some time, it won’t seem as complicated as you once thought.
Visual Basic (VB) was created by Microsoft and was released in 1991. Although many versions have been created since that release, it is still considered to be one of the easiest programs to learn and is especially one of the easiest programs to use to write programs for Windows. Most people find it is also fun to use.
There are three different types of VB:
In 2002, Microsoft replaced VB with VB.NET as the successor to the VB language. There is talk of VB being retired as other programming options exist, such as C#, Java, and C##, which are more popular programs among serious developers.
Let’s discuss creating code within Outlook by using the built-in VBA editor to create a macro. Within the editor, you can create code for a macro using VBA, which can include the use of objects, properties, methods, and events, as briefly described in this chapter.
Using VBA within Outlook, you will find, makes it easy to complete repetitive tasks and have the application behave according to your expectations. VBA code that is written to perform a task is called a macro. You can program macros to be as simple or complex as needed, as well as being able to program them to run automatically or with the click of a button, or simply to have something displayed.
You may be asking what is the difference between a macro and VBA? A macro is programming code, which happens to be written in VBA, that runs on application platforms such as Outlook, Excel, Word, and others to perform automatic routine tasks. VBA is the programming application built within Outlook (or other apps) that is used to create macros.
In Office applications such as Excel and Word, you can record macros using a simple tool, Record Macro. This tool will create the VBA code for you as you go through the steps on the screen while having the code generated and saved as a macro.
The record macro tool does not exist in Outlook as it does in Word and Excel. In Outlook, you must write the code in the VBA editor, which means it is necessary for you to know how to use the VBA editor, and then you’ll need to know how to incorporate it into Outlook. Knowing how to write code in VB will help you to write code in VBA.
Let’s start the steps necessary for writing code by turning on the Developer tab.
The Developer tab is hidden in Outlook and other Microsoft applications such as Excel and Word. It is most useful for writing VBA code within these applications. It does have other uses, such as running macros, using XML commands, using ActiveX controls, and creating applications to have fun with several applications. In my opinion, using the Developer tab is most useful for creating macros as well as using form controls.
Let’s activate the Developer tab:
With the Developer tab activated, you can view, create, and run macros using the tools on the ribbon. Let’s now use the Visual Basic Editor to create a macro.
Visual Basic Editor is used to create code in Outlook and other Microsoft applications. It is most useful for non-programmers. It is built into the applications and can be used by activating the Developer tab:
Alternatively, you can press Alt + F11 to open the Visual Basic Editor. This view may also be called the VBA developer Integrated Development Environment (IDE).
You will also want to be sure that you have activated the object model when writing the code from within Outlook or another application:
Now you are ready to write code in the VBA editor window.
For this example, we will create an Outlook macro to allow us to click on a button on the toolbar to have an email created from a template or created to reply with a template to an existing email that has been received. If you find yourself writing the same email repeatedly, you will benefit from this type of macro. Before we get started with this macro, we need to first create the email and save it as a template:
Let's go through the process of creating a macro:
The following is the code for the new email template:
Sub NewEmailTemplate()
Set msg = Application.CreateItemFromTemplate("<FILEPATH HERE><FILENAME HERE>.oft")
msg.Display
End Sub
Sub ReplyEmailTemplate()
Dim origEmail As MailItem
Dim replyEmail As MailItem
Set origEmail = Application.ActiveWindow.Selection. Item(1)
Set replyEmail = Application. CreateItemFromTemplate("<FILEPATH HERE> <FILENAME HERE>.oft")
replyEmail.To = origEmail.Sender
replyEmail.CC = origEmail.CC
replyEmail.Subject = origEmail.Subject
replyEmail.HTMLBody = replyEmail.HTMLBody & origEmail. Reply.HTMLBody
replyEmail.Display
End Sub
Note
Comments are entered into the code with an ‘(apostrophe) before the text, which will turn the text green. These comments are for explaining the code that is written below. This is a very useful coding technique to help describe what the code is written to do.
Now we need a way to run the macros. We could choose the Macro button from the Developer tab:
An easier way would be to create a button on the ribbon. I will place it as a new button on the Home tab in the Respond section.
When the New Volunteer button is clicked, a new email will generate the template with the header information filled in along with attached documents and the body of text. You can add the name of the person receiving the email in the header as well as the body of the email and click Send.
To use the Reply button, select but don’t open an email that you wish to reply to with the template email created in the second macro that was created in the VBA editor. This will generate an email with the selected template and you will see the header information already filled in. Add the name of the person you are sending the message to in the body of the text, verify that you don’t want or need to make any changes, and click Send.
These are two very useful templates that I have found save me a lot of time with new clients.
Note
Macros are one way for malicious code to access and run malware and ransomware. To help improve security, Microsoft has announced that beginning in April of 2022, the default for running macros will be to block macros in files from the internet. This means any emails with macros included will be blocked and a security risk message will indicate that it was blocked.
The following are some suggestions of what you could create macros for within Outlook:
These are only a few tasks that you could use macros for. I would suggest that you start watching the way you use Outlook and when you find yourself repeating steps over and over, creating a Macro for that repetition could save you time.
VBA is a very powerful programming solution for many problems, but keep in mind that it is not the only way. You may find other solutions. Quick Steps, for example, for many can replace the need for macros and there would be no coding necessary for these to run. Ask yourself as you choose a feature, is there an easier way? Take the time to evaluate all your options. Programming requires focus and can be very time-consuming and unpredictable. If you are a beginner, using the VBA editor is a great way to learn. Be sure you have the time to work it all through. If a deadline is approaching, it can make for a stressful situation, and you may want to do a repetitive action first and create the code after things settle down.
In the next chapter, we will discuss the Manage Your Day system. This is a system that I use with some of my favorite tools, tips, and tricks that can help you gain control of Outlook. If you would like to streamline your daily activities, which will have you working SMART with Outlook in no time at all, I highly recommend you keep reading.