Many of us have used the feature in Outlook that allows you to open a contact item and create a new appointment for the contact. I was always disappointed in this feature because, while it did create the appointment, it did not insert any of the contact information into the appointment. It only added the contact as the “contact” for the appointment. This meant that if you printed your appointments, you could not see things like the contact’s address and phone number unless you copied and pasted them into your new appointment.
To overcome this limitation, I recently wrote a simple macro that will not only create the appointment item but also insert the contact name and phone number into the appointment Subject field. It will also insert the contact name, address, and phone numbers into the Notes field. While this macro is small (under 30 lines), it gets the job done and offers a good example of how to do simple but useful macros in Outlook.
To give you a better understanding of how this macro works, I have provided a section-by-section description.
Dim txtBody1 As String
Dim txtBody2 As String
Dim txtBody3 As String
Dim objCurContact As ContactItem
Dim objAppt As AppointmentItem
On Error GoTo ErrorCode
Here, we declare three text variables. These will hold the contact values to be inserted into the new appointment item. While it is possible to insert these values directly into the appointment item, it is less confusing for those new to VBA to see the values placed into a variable and then from the variable into the target item. This is especially true when fields and text are being concatenated or joined together. We also have two object variables, one for the contact item and one for the appointment item. Last in this section of code is the “On Error…” statement that will allow us to trap any errors that might crop up and to provide a more graceful handling of these errors.
In section 2, the “If Application…” line is our conditional that tests the currently open Outlook item to see if it is a contact item. To do this, we use the Class property of the ActiveInspector.CurrentItem object. If the Class equals olContact, then our code will continue. As we will see later, if the Class is not a Contact, the code will display an error message. Next, we see two Set statements, in which the code is assigning objects to the two object variables we declared earlier. The objCurContact is set to represent the CurrentItem (the currently open Contact item) and objAppt is set to represent a newly created item by setting it as Application.CreateItem(1). The 1 is an appointment item.
The next line, “txtBody1 =…” is where the code concatenates the Contact Full Name, Job Title, and Business Address into a single variable. The code inserts carriage returns between these items by using the chr(13) ASCII code.
Section 3 starts by setting the txtBody2 variable to equal the contact Business phone and Mobile phone numbers. This text variable also uses Chr(9) ASCII codes to insert tabs between the text describing the phone numbers and the phone numbers themselves. This way, the numbers are in neat columns rather than just strung together on the line.
Next is the line where the txtBody3 variable is set to equal the Contact Body field. Here, you might store free text notes about a contact. These notes might come in handy inserted into the appointment item. This section then sets the new appointment item Subject field to equal the Full Name and Company Name of the Contact.
The next line, objAppt.Body = txtBody1 & txtBody2 & txtBody3, concatenates the three text variables together and inserts them into the Body field of the new appointment. The next line displays the new appointment on screen.
Section 4 is the rest of the “If Application…” statement from above. It starts with the Else portion that lets us specify an action to be done if the test is found to be false. In this case, it lets us display a message box if the macro is run and the CurrentItem is not a Contact item. That is what the “MsgBox…” statement does on the next line. Of course, the End If tells our macro when the “If…Then” is complete.
This final section is the error-handling section. The Exit Sub statement is important so that the code that follows it is only executed if there is an error. You cannot run this macro by just selecting a contact. The item must be double-clicked and opened. The part that starts with “If Err.Number = 91 Then” responds to a specific error (91) that will be raised if the macro is run when no item is currently open. This section of code will detect if there is no item open and display a notification message. The next section of code will respond to any other error that comes up.
Adding the macro to your installation of Outlook
Copy the macro from the sections above into a text document and then open the Outlook Visual Basic Editor by selecting Tools | Macro | Visual Basic Editor. Next, you must choose Insert | Module. This will open a new, blank module. Paste the macro text into this module.
Using the macro
The use of this macro is straightforward. You open a contact item and then run the macro. That is it! Remember that the item must be open. You cannot just select a contact. You must open it by double-clicking on it and then running the macro. To run the macro, simply select Tools | Macro | Macros and then pick the Appt_From_Contact macro from the list.