Category: Code

Macro Outlook 2019 - automatic Greeting

Office 365 applications accept Visual Basic code to execute custom actions. Seeking to automate the greeting start of the emails I present this solution that provides us automatically the following result at the start of a response mail:

Estimado/a Nombre Apellido,
Buenos días,

The surname is sometimes replaced by the middle name to avoid cutting out compound names, which are very common in Spanish. Names like Juan Diego, Juan Javier, María Paula, María Belén, etc.

Depending on the time of day the message on the second line changes from "Good morning", "Good afternoon" and "Good evening".

This code was saved in a macro, and placed in between the Outlook tools for quick access via keyboard.

When we receive an email we can respond automatically.
A new icon in the 'ribbon' represents the new macro action.
The automatic message auto calculates the values in order to automate the message header.

Below is the source code for this macro:

Sub AutoGreeting()

Dim oMItem As Outlook.MailItem
Dim oMItemReply As Outlook.MailItem
Dim sGreetName As String
Dim sGreetTime As String

On Error Resume Next

Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set oMItem = ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set oMItem = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0

If oMItem Is Nothing Then GoTo ExitProc
On Error Resume Next

sGreetName = oMItem.SenderName
Set oMItemReply = oMItem.Reply
Select Case Time
Case Is < 0.5
sGreetTime = "Buenos días,"
Case 0.5 To 0.75
sGreetTime = "Buenas tardes,"
Case Else
sGreetTime = "Buenas noches"
End Select
With oMItemReply
.HTMLBody = "<span style=""font-size : 10pt""><p>Estimado/a " & Split(sGreetName)(0) & " " & Split(sGreetName)(1) & ",<br>" & sGreetTime & "</p></span>" & .HTMLBody

End With

Set oMItem = Nothing
Set oMItemReply = Nothing
End Sub