Chapter 15: Programming with Macros

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:

  • The Outlook object model
  • The Visual Basic Application
  • Macros

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.

The Outlook object model

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:

  • Application – Represents the entire application
  • AppointmentItem – A new appointment used with the CreateItem method
  • ContactItem – A new contact object, used with the CreateItem method
  • Explorer – Displays the contents of a folder and where displayed
  • Folder – Represents a folder that contains email
  • Inspector – Represents the Outlook item that displays when you double-click an item
  • MailItem – Represents a new mail item in a folder
  • TaskItem – Represents a task within a task folder

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:

Table 15.1 – Objects and components

Table 15.1 – Objects and components

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

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:

  • Visual Basic (VB) – Best for beginner programmers or novices to create their own Windows applications.
  • Visual Basic Script (VBScript) – Used for Windows scripting and web page scripting for use in Internet Explorer or Edge.
  • Visual Basic for Applications (VBA) – Used for all Microsoft Office applications, such as Outlook, Excel, Word, PowerPoint, and Access. It runs within the application rather than as a standalone product.

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.

Macros

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.

Activating 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:

  1. To activate the Developer tab, click File | Options | Customize Ribbon. Check the Developer box and click OK.
Figure 15.1 – Turn on the Developer tab

Figure 15.1 – Turn on the Developer tab

  1. Locate the Developer tab, which will now be displayed in the menu.
Figure 15.2 – Developer tab

Figure 15.2 – 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

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:

  1. Click Developer | Visual Basic to open the Visual Basic Editor window:
 Figure 15.3 – Open Visual Basic Editor

Figure 15.3 – Open Visual Basic Editor

  1. The following Microsoft Visual Basic for Applications window will open, and you can start creating your macros in this window.
Figure 15.4 – Microsoft Visual Basic for Applications

Figure 15.4 – Microsoft Visual Basic for Applications

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:

  1. Click Tools | References… and check the box next to Microsoft Office 16.0 Object Library. You may have a different version number depending on your installed version of Outlook. Click OK.
Figure 15.5 – Activate Object Library

Figure 15.5 – Activate Object Library

Now you are ready to write code in the VBA editor window.

Macro scenario

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:

  • Create the email template – Create an email as normal but do not send; save it as a template. Be sure to include any attachments, To, Cc, or Bcc headings, and messages in the body of the email. Do not include a signature if you have a signature automatically populate when you start a new email. Just delete the signature from this email before saving it as a template. The steps to create a template are detailed in Chapter 16, Managing Your Day System, step 7.

Creating a macro

Let's go through the process of creating a macro:

  1. In the VBA editor, click ThisOutlookSession. Create Macro in a blank box or referred to as Module. The code for the two macros is as follows.

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

  1. The following is the code for the reply email template:

    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

  2. To copy your own template location to paste over the <FILEPATH HERE> and <FILENAME HERE> text within the code, click Home | New Items | More Items | Choose Form…:
Figure 15.6 – Choose Form template

Figure 15.6 – Choose Form template

  1. From the Choose Form dialog box, choose the drop-down arrow for the Look In textbox and select User Templates in File System | Open.
Figure 15.7 – User Templates in File System

Figure 15.7 – User Templates in File System

  1. Select the template location from above the files listed in the Choose Form dialogue box, not including the *.oft filename.
Figure 15.8 – File template location to copy

Figure 15.8 – File template location to copy

  1. You have now created the two macros and they should look as follows, with your data in <FILEPATH HERE> and <FILENAME HERE> filled in with your file path and filename from your computer.
Figure 15.9 – Completed macros

Figure 15.9 – Completed macros

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.

Running macros with the button on the ribbon

Now we need a way to run the macros. We could choose the Macro button from the Developer tab:

Figure 15.10 – New macros created

Figure 15.10 – New macros created

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.

Figure 15.11 – Respond section on the Home tab

Figure 15.11 – Respond section on the Home tab

  1. Click File | Options | Customize Ribbon | Expand Respond. Under Home in the Main Tabs box on the right of the Customize the Ribbon window, select Respond | New Group | Rename….
Figure 15.12 – New Group name

Figure 15.12 – New Group name

  1. Enter a new name for the group in the Display name text field and click OK to confirm the renaming. In the box on the left below the Customize the Ribbon title, click the dropdown and select Macros, and drag each macro to the New Group item created, as shown in the following figure. Click OK:
Figure 15.13 – Macros applied to the new group

Figure 15.13 – Macros applied to the new group

  1. Select each macro and rename the macro to the name to be displayed within a button on the ribbon. Click OK. Once this is done, you will find the buttons on the ribbon, as shown here:
Figure 15.14 – New buttons for new emails from templates

Figure 15.14 – New buttons for new emails from templates

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:

  • To export contacts to a database
  • To save attachments from an email to a folder on your computer
  • To update meeting information automatically for recurring appointments
  • To download all attachments from a specific Outlook folder to a folder on your computer
  • To send Outlook emails from Excel and Word
  • To create automatic responses to meetings

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.

Summary

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.

Questions

  1. Is VBA available in the current Microsoft 365 subscription?
  2. How can I enable macros? I keep getting a message that my macros are disabled.
  3. How can I back up my macros?
  4. How do I open the VBA editor within Outlook?
  5. Will macros be included in the MOS Outlook exam?

Answers

  1. VBA is currently supported in the desktop version of Microsoft 365 and will be for some time in the future. However, keeping it working on all versions of Outlook will probably not happen. As of the writing of this book, there are no plans to retire VBA. Microsoft will continue to encourage users to learn JavaScript APIs as the VBA replacement.
  2. Macros can pose a threat to your Outlook environment and Microsoft has now set the default to disable macros coming from the internet. To change this setting, click File | Options | Trust Center | Trust Center Settings… | Macro Settings and select the desired setting.
  3. Back up or create a Word document with your Outlook macros. Open the VBA editor in Outlook and copy the code for the macros. Paste the copied code into a Word document and save it to your computer.
  4. The shortcut key for opening the VBA editor within Outlook is Alt + F11.
  5. The current MOS Outlook exam is exam MO-400. Macros are not included in the objective domains for this exam. See Further reading for the MO-400 exam objectives.

Further reading

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset