Microsoft

Use this script to quickly configure Outlook signatures

Would you like to add a special note to your Outlook signature each week--like a reminder to your staff? Here's a script that lets you modify your signature without having to drill down through layers.

You’d probably rather not waste time hunting for application options that are inconveniently placed deep in menus. For example, editing your Outlook signature requires that you drill down to Tools | Options | Mail Format | Signature Picker | Edit. You can't even add an Edit Signature command (or many other Outlook commands) to a customized menu bar.

Fortunately, you can use Windows Script Host to automate these repetitious tasks. In this article, I'll show you a simple script that automates Edit Signature. You can use it as a template to create other automations for Outlook or any other programs using a Windows OS.

Why use Windows Script Host?
My editsig.vbs script uses Windows Script Host (WSH) to send the necessary keystrokes to Outlook. WSH scripts process instructions more or less sequentially; they're used to automate tasks. Programmers who have been around awhile will recognize their similarity to DOS batch files and Windows 3.1 Recorder macros.

While third-party tools for creating macros exist, WSH has many advantages. Among them: It's free, it's relatively easy to use, and it's a cross-Windows environment. WSH scripts are text files written in either a JScript or VBScript syntax and ending in a .js or .vbs file extension. To execute these files, you simply double-click them.

Installing and learning WSH
Before creating your script, download and install the latest version of Windows Script Host (5.6 as of this writing) from Microsoft's Windows Script site. You'll find tutorials there too. While you're at it, also download the documentation.

Create editsig.vbs
Editsig.vbs is a simple script of only 18 lines (see Listing 1). Use it as is, or modify it as needed to conveniently access other application functions buried within menus. To create editsig.vbs, copy the listing to Notepad or your favorite text editor. If you use Word or another editor, make sure to save it as a text file. (Note to beginning programmers: Don't copy the line numbers.)

Listing 1
  1. ' FILE: editsig.vbs 
  2. ' DESC: Drill down through the Outlook menus to open edit signature
  3. ' AUTH: Michael Jackman
  4. '
  5.  
  6. Set WshShell = WScript.CreateObject("WScript.Shell")
  7. WshShell.AppActivate "outlook"
  8. ' WshShell.Run "outlook", 9
  9. Wscript.Sleep 500
  10. WshShell.SendKeys "%T"
  11. WshShell.SendKeys "o"
  12. WshShell.AppActivate Options
  13. Wscript.Sleep 150
  14. WshShell.SendKeys "^{Tab}"
  15. WshShell.SendKeys "^{Tab}"
  16. WshShell.SendKeys "%G"
  17. WshShell.SendKeys "%E"
  18. Wscript.Quit

Add a program and keyboard shortcuts
Open Windows Explorer, right-click editsig.vbs, and choose Create Shortcut. Then drag the new shortcut to your Start menu. Now right-click it and choose Properties. Click the Property sheet's Shortcut Tab, then click inside the Shortcut Key textbox. Press the CTRL-ALT combination you want to use to execute the script, and it will appear inside the box.

Understanding editsig.vbs
Three WSH methods do the grunt work of this script: WshShell.AppActivate (or WshShell.Run), Wscript.Sleep, and WshShell.SendKeys. Bookending these methods are a few lines of description and two other important methods. Set WshShell = WScript.CreateObject("WScript.Shell") initializes the WScript Shell (Line 6), while Wscript.Quit terminates the script.

WshShell.AppActivate and WshShell.Run
Editsig.vbs works when Outlook is already running. Since you'll most likely have Outlook open when you want to modify the signature, that shouldn't be a terrible limitation.

WshShell.AppActivate (Line 7) looks for an open application whose title bar contains the text that matches a string—in this case, an application title with the word Outlook in it. It then selects that application.

Use care when naming strings
Note that if both Outlook Express and Outlook are open at the same time, this script won't know the difference and will choose between them arbitrarily. While running the script on Express won't do any harm (it won't do any good, either), be careful how you set strings for other applications. A harmless key sequence in one program can be destructive in another.

In Line 8, I've commented out an alternative to WshShell.AppActivate, called WshShell.Run. This method runs the command specified by the string as a new process, as if you typed it in Start | Run. You can use this method if you want a script to activate Outlook when it's not open. The 9 tells WshShell.Run to open the application and display a window. To use this method, just comment out Line 7 and delete the comment from Line 8. DevGuru's Web site has more information on the settings for the Run method.

Wscript.Sleep
Wscript.Sleep (Lines 9,13) pauses the script for the specified number of milliseconds in order to give the application time to respond. On my computer, editsig.vbs needed about half a second to activate Outlook and about a tenth of a second to switch to the Options menu. You may need to adjust these values for your application.

WshShell.SendKeys
SendKeys passes the keystrokes to the application. Here, the keys are [Alt]T, which opens the Tools menu; O to select Options; two [Ctrl]Tabs to switch to the Mail Format tab; followed by [Alt]G and [Alt]E to open the Signature Picker and Edit screens.

As you can see in the listing, special characters are indicated by % for [Alt], ^ for [Ctrl], and braces for other keys, such as [Tab]. DevGuru has a complete list of key codes for the SendKeys method.

Create your own timesaving script
To use Editsig.vbs as a template, first jot down the keystrokes needed to activate a command. Then modify the SendKeys sequence, using Wscript.Sleep when necessary to give the application time to process commands. Finally, replace the AppActivate or Run string with one that starts the program of your choice.

If you want to take this script further, you might want to add menus, or even a function that checks whether an application is running, then switches between Run or AppActivate depending on the result.

More resources
For more information about using Windows Script Host, read the following articles by TechRepublic author and WSH guru Greg Shultz (whose help in preparing this article is greatly appreciated):

(Note: These articles are available on TechPro Guild. If you’re not already a member, you can sign up for a free 30-day trial membership by clicking any of these article links.)

In addition to Microsoft's script site, you can find valuable resources on WSH objects, methods, and properties at MSN's Windows-Script group and the DevGuru WSH quick reference site.
0 comments

Editor's Picks