Friday, 14 October 2016

Add Secure to the Message Subject before Sending

You can either use an ItemSend macro to add Secure automatically, or use macro to selectively add Secure to the message.

ItemSend

VBA
9 lines
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSecure As String
strSecure = "Secure " & Item.Subject
Item.Subject = strSecure
End Sub
To use ItemSend and add Secure to the subject of some, but not all, messages, you need to use an If statement. If the message meets the condition, Secure is added to the subject.
In this example, we're testing for domains in the address. If a match is found, the subject is edited and sent. To send to all EXCEPT some domains, use
If InStr(LCase(strRecip), arrDomain(n)) = 0 Then.
VBA
29 lines
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Recipients As Outlook.Recipients
Dim recip, strRecip As String
Dim i, n
Dim arrDomain As Variant
Dim strSecure As String
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
strRecip = recip.Address & " " & strRecip
Next i
' Set up the array
arrDomain = Array("domain1.com", "domain2.com")
' Go through the array and look for a match, then do something
For n = LBound(arrDomain) To UBound(arrDomain)
If InStr(LCase(strRecip), arrDomain(n)) > 0 Then
strSecure = "Secure " & Item.Subject
Item.Subject = strSecure
Exit Sub
End If
Next n
End Sub

Macro Button on Ribbon

To use this macro, customize the message ribbon by adding a new button near the existing Send button.
Add Secure to Subject
VBA
13 lines
Sub AddSecure()
Dim Item As MailItem
Set Item = Application.ActiveInspector.CurrentItem()
Dim strSecure As String
strSecure = "Secure " & Item.Subject
Item.Subject = strSecure
Item.Send
End Sub

Add Secure to Reply

Like the previous macro, you'll create a button on the ribbon for this macro. Click the button instead of Reply.
VBA
14 lines
Public Sub SecureReply()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Dim strSecure As String
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
Set Item = objItem.Reply
strSecure = "Secure " & Item.Subject
Item.Subject = strSecure
Item.Display
End Sub

How to use Macros

First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s atTools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low orsign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To use the macro code in ThisOutlookSession:
  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)

Replace Display Names with Email Addresses

In a long thread discussing the problem of Outlook not showing the recipients email address atHow to show email address not just name in From and To fields a user mentioned the steps he uses to get around this problem:
I need the full email address displayed when I print a sent email so that I can prove exactly what email address it was sent to. So instead of TO: 'John Doe' it will print out as john@domain.com. Right now when replying to an email, or starting a new one, I have to double click on 'John Doe ' and copy the full email address and paste it into the TO field. Otherwise it will print out as John Doe.
print header with names
Outlook uses the display name in the To/CC/BCC fields, getting it from the message you are replying to, or from the Email Display Name field in Contacts. Newer versions of Outlook will use Full Name (email@domain.com) as the display name format in Contacts but senders almost always use their name as the display name and don't include the email address. While newer versions of Outlook display the email address when you are composing mail, when the message is sent, only the display name is visible.
This specific problem is easy to solve using an ItemSend macro. Before the message is sent, the macro changes display names to the underlying email address. If the recipient entry contains an @ sign, it's skipped (remove the If/End If lines if you use @ signs in display names). It's completely automatic - as soon as you press Send, the display names are changed to the email address.
print header with addresses
VBA
18 lines
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim i
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set R = Recipients.Item(i)
' if the entry is already an address, skip it
If InStr(1, R, "@") = 0 Then
Recipients.Add R.Address
Recipients.Remove i
End If
Next
Recipients.ResolveAll
End Sub

How to use the macro

First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or above, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s atTools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.

To use the macro code in ThisOutlookSession:
  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)

Running Outlook Macros on a Schedule

Outlook doesn't have a timer function but you can use Appointment or Task Reminders to trigger macros. Set up an Application_Reminder macro that will do something when a reminder fires. To limit it to running when specific reminders fire, use an If statement to look for words in the subject or a specific category.
If you want the macro to fire a specified time after you restart Outlook, use an Application_Startup macro to create the appointment.
Create an appointment reminder to trigger a macro
If you need to rerun the process kicked off by the reminder, run CreateAppointment macro from the VBA editor at any time.
VBA
62 lines
' The Private subs go in ThisOutlookSession
Private WithEvents olRemind As Outlook.Reminders
Private Sub Application_Startup()
CreateAppointment
End Sub
Private Sub Application_Reminder(ByVal Item As Object)
Set olRemind = Outlook.Reminders
If Item.MessageClass <> "IPM.Appointment" Then
Exit Sub
End If
If Item.Categories <> "Run in 5" Then
Exit Sub
End If
' Call your macro here
MsgBox "It works!"
'Delete Appt from calendar when finished
Item.Delete
' Create another appt to repeat the process
CreateAppointment
End Sub
' dismiss reminder
Private Sub olRemind_BeforeReminderShow(Cancel As Boolean)
For Each objRem In olRemind
If objRem.Caption = "This Appointment reminder fires in 5" Then
If objRem.IsVisible Then
objRem.Dismiss
Cancel = True
End If
Exit For
End If
Next objRem
End Sub
' Put this macro in a Module
Public Sub CreateAppointment()
Dim objAppointment As Outlook.AppointmentItem
Dim tDate As Date
' Using a 1 min reminder so 6 = reminder fires at 5 min.
tDate = Now() + 6 / 1440
Set objAppointment = Application.CreateItem(olAppointmentItem)
With objAppointment
.Categories = "Run in 5"
.Body = "This Appointment reminder fires in 5"
.Start = tDate
.End = tDate
.Subject = "This Appointment reminder fires in 5"
.ReminderSet = True
.ReminderMinutesBeforeStart = 1
.Save
End With
End Sub

How to use the macro

First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s atTools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.

To use the macro code in ThisOutlookSession:
  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Application_Startup macros run when Outlook starts. If you are using an Application_Startup macro you can test the macro without restarting Outlook by clicking in the first line of the Application_Startup macro then clicking the Run button on the toolbar or pressing F8.
To put the code in a module:
  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.