This post goes out to John Cook, a gent I met on Twitter. As you can see from John’s tweet he’s looking for a way to add a button to the ribbon at the top of an Outlook message. When clicked the button should encrypt the message.
I’ve put together a potential solution for John, but can’t test it as I don’t have a computer with Outlook 2007 and a certificate. The ribbon in Outlook 2007 isn’t as friendly as the one in 2010, so instead of adding the button to the ribbon my solution adds it to the QAT (quick access toolbar). For those of you who aren’t familiar with the QAT, it’s the icon area on the upper-left of each Outlook window. This Microsoft page explains the QAT in more detail.
The code I’m using for this solution comes from Microsoft KB article #279013, “OL2000: How to Programmatically Sign or Encrypt Mail Messages”. The article describes making a custom form with code behind it that sings/encrypts the message when it’s sent. I’ve adapted the code so it doesn’t require a custom form and is run in typical macro fashion, in this case in response to a QAT button click.
The code itself uses the old Outlook trick of finding and simulating a click on a hidden toolbar button. Up until 2010 simulating clicks on hidden buttons was a way to work around some of the limitations of Outlook’s object model. Outlook 2010 eliminated this approach, so this code will not work with 2010 or later.
Since I do not have a copy of Outlook 2007 with a certificate I cannot test the code to ensure it works. It should work, but I can’t say for certain that it will. John will have to test it for himself to determine that.
Adding the Code to Outlook.
- Start Outlook
- Press ALT+F11 to open the Visual Basic Editor
- If not already expanded, expand Microsoft Office Outlook Objects
- If not already expanded, expand Modules
- Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
- Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
Option Explicit Sub EncryptMessage() Dim oDigSignCtl As Object, oCBs As Object If TypeName(Application.ActiveWindow) = "Inspector" Then Set oDigSignCtl = Application.ActiveInspector.CommandBars.FindControl(, 718) If oDigSignCtl Is Nothing Then ' Add the toolbar button to the item. Set oCBs = Application.ActiveInspector.CommandBars Set oDigSignCtl = oCBs.Item("Standard").Controls.Add(, 718, , , True) End If ' Check to make sure the button is not dimmed. If oDigSignCtl.Enabled = True Then ' Check to make sure the button is not depressed. If oDigSignCtl.State = 0 Then oDigSignCtl.Execute Else MsgBox "Encryption impossible. You do not have a digital signature!", vbCritical + vbOKOnly, "Encrypt Message" End If End If Set oCBs = Nothing Set oDigSignCtl = Nothing End Sub
Adding the Button to the QAT.
This page describes how to add a button to the QAT that runs a macro.