Tuesday, April 2, 2019

An Outlook Macro

Objective: To create a macro for Outlook 365 which will paste a text string when a hotkey is pressed. In this example, the string '@brightidiaz.com' is pasted when the hotkey Alt-6 is pressed.

STEP 1 - Enable Macro


Click on the Customize Quick Access Toolbar ribbon as shown. Select More Commands.

Select Trust Center then Trust Center Settings. This will open a new window, select the Macro Settings suitable. I recommend 'Notification for all macros' to be on the safe side. Click on OK to accept the Macro setting. Click the OK button again to accept the Trust Center setting. This has been marked on the screenshot above and color-coded for easy reference.

STEP 2 - Enable the Developer option

Click on the Customize Quick Access Toolbar ribbon as with STEP 1 and select More Commands. Click on Customize Ribbon and see to it that the option Developer is checked. Accept the change by clicking on OK.

 STEP 3 - Write the Macro

 
Click on the Developer tab then click on the Visual Basic button.

 
The Microsoft Visual Basic for Applications project window will open up as shown above. Navigate to ThisOutlookSession as shown encircled in red. This will open the code window shown at right above.


Paste the following code inside the code window.

Sub InsertText()
Const sText As String = "@brightidiaz.com"
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
    If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
        ActiveInspector.WordEditor.Application.Selection.TypeText sText
    End If
End If
Exit Sub
ErrHandler:
Beep
End Sub

The code window will look like as shown above. Click on the Save button encircled in the screenshot above and CLOSE the window.

STEP 4 - Create a button for the Macro


Click on New Email as if composing a new email. Click on the Customize Quick Access Toolbar in the new email window then click on More Commands.



Click on the Customize Quick Access Toolbar as in STEP 1 or click on File | Outlook Options which will achieve the same thing. Choose Quick Access Toolbar then select Macros from the Choose commands from drop down. Highlight the name of the Macro Project1.ThisOutlookSession.InsertText then click on Add to add it to the right. Click OK to accept the changes. This has been marked on the screenshot above and color-coded for easy reference.


A button will be created on top of the new email window, encircled in yellow shown above. Click on this button each time you need to insert the text defined in the Macro.


STEP 5 - KEYBOARD SHORTCUT

Press the ALT key to determine the keyboard shortcut assigned for the macro. In this example, the keyboard shortcut assigned is ALT-6. Thus, pressing the key combination of ALT and 6 will insert the text defined in the macro.