Wednesday, June 01, 2005

Am I Lazy or What?

Sunday's Dilbert cartoon might strike a nerve in some engineers. But why? A good engineer is a lazy engineer. The computer was invented because Charles Babbage got tired of calculating logarithms by hand.

Having written that, I'm proud to announce that I spent the last three hours automating something that used to take about five seconds. Yes, I know, but I do it a lot. Those five seconds add up. And now it only takes one second.

Here's the deal. My employer upgraded Outlook from 2000 to 2003 yesterday. But it didn't upgrade the other Office applications. I had used Word as my Outlook message editor, and I used its AutoText feature to add signatures to my emails. Since Outlook 2003 doesn't use Word 2000, I was forced to investigate Outlook's signature facility.

Outlook was thoughtful enough to allow for multiple signatures, and I thank the developers for that. But they failed to allow a proper method for selecting them with a keystroke. I was able to use keys, but look at the sequence involved: Alt-I S M x ENTER (where x is the first letter of a signature name.)1

So I wrote a macro that allows me to choose one of my four signatures from a listbox. So now it's either Alt-S ENTER (for the default signature) or Alt-S x ENTER, a savings of two or three keystrokes. And that means extra time for blogging. :-)



The code for this automation can be entered into two text files called module1.bas and userform1.frm. I recommend first adding the module and creating the user form in the VBA editor. Add a listbox and two command buttons to the form. Their names should be listbox1, commandbutton1 and commandbutton2, and the form should have the name userform1.

Here's what should go into module1.bas:
Option Explicit

' Supporting code for selecting message signatures.
' Copyright 2005 - 2006 by Luddite Geek, luddite.geek@sbcglobal.net

Sub SelectSig()
Load UserForm1
UserForm1.Show

End Sub

Function HTMLize(strBody As String) As String
' Replaces vbCrLf with <br />
HTMLize = Replace(strBody, vbCrLf, "<br />")

End Function


Here's what should go into userform1.frm:
Option Explicit

' Signature Chooser Code
' Copyright 2005 - 2006 by Luddite Geek, luddite.geek@sbcglobal.net

Private Sub CommandButton1_Click()
' Based on http://www.outlookcode.com/codedetail.aspx?id=141

    Dim objItem As Object
    Dim thisMail As Outlook.MailItem
    'On Error Resume Next
    
    Set objItem = Application.ActiveInspector
    If Not objItem Is Nothing Then
        If objItem.CurrentItem.Class = olMail Then
            Set thisMail = objItem.CurrentItem
            If thisMail.HTMLBody = "" Then
                thisMail.Body = thisMail.Body & ListBox1.Text
            Else
                thisMail.HTMLBody = thisMail.HTMLBody & HTMLize(ListBox1.Text)
            End If
        End If
    End If
    
    Set objItem = Nothing
    Set thisMail = Nothing
    
    UserForm1.Hide
    Unload UserForm1
    
End Sub


Private Sub CommandButton2_Click()
    UserForm1.Hide
    Unload UserForm1

End Sub


Private Sub UserForm_Initialize()
UserForm1.Caption = "Luddite Geek Signature Chooser"
CommandButton1.Caption = "OK"
CommandButton1.Default = True
CommandButton2.Caption = "Cancel"
CommandButton2.Cancel = True

ListBox1.ColumnCount = 2

ListBox1.AddItem "Work"
ListBox1.List(0, 1) = vbCrLf & _
                      "Work Signature Line 1" & vbCrLf & _
                      "Work Signature Line 2" & vbCrLf & _
                      "Work Signature Line 3" & vbCrLf & _
                      "Work Signature Line 4"

ListBox1.AddItem "Home"
ListBox1.List(1, 1) = vbCrLf & _
                      "Home Signature Line 1" & vbCrLf & _
                      "Home Signature Line 2" & vbCrLf & _
                      "Home Signature Line 3" & vbCrLf & _
                      "Home Signature Line 4"

ListBox1.AddItem "Blog"
ListBox1.List(2, 1) = vbCrLf & _
                      "Blog Signature Line 1" & vbCrLf & _
                      "Blog Signature Line 2" & vbCrLf & _
                      "Blog Signature Line 3" & vbCrLf & _
                      "Blog Signature Line 4"

ListBox1.TextColumn = 2
ListBox1.ColumnWidths = "60;0"
ListBox1.SetFocus
ListBox1.ListIndex = 0

End Sub


1 The keystroke sequence in Outlook 2010 is worse. It's Alt-N AS S and then you need to use the arrow key to select from the list of signatures. Pressing the first letter of the signature name no longer selects it.


Edited on 2006-07-06 to add requested code samples.
Edited on 2013-08-05 to update link to Dilbert cartoon and footnote 1.

No comments: