Thursday, January 22, 2009

Create a default Outlook signature from AD with vbscript

This site has some great vbscript to create an Outlook signature based on information from Active Directory. It automatically generates a .txt .rtf and .htm signature. To eliminate the extra spacing, add this to the script after
Set objSelection = objWord.Selection

objSelection.Style = "No Spacing"

This is great if you want to force your users to have standardize Outlook signatures and to add a disclaimer / confidentiality notice.

This works great for Outlook 2007 clients, but in Outlook 2003 I get double spaced signatures. I can't figure out a way around this without using registry entries to switch to .txt or .rtf signatures. Outlook 2003 doesn't know what "No Spacing" is. Looking at the generated html, there's a no spacing class p class=MsoNoSpacing created by the Outlook 2007 clients. The Outlook 2003 clients also don't know about that class. I need to stop it from using html paragraph tags for each line.


I finally have a script that works both with Office 2003 and 2007. objSelection.Style = "No Spacing" takes care of 2007, but for 2003 you need to use Chr(11) instead of vbNewline.

Here's my final script:

On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

strConfid = "Confidentiality Notice: blah blah blah"
strFname = objUser.FirstName
strLname = objUser.LastName
strInitial = objUser.Initials
strTitle = objUser.Title
strDepartment = objUser.Department
strCompany = objUser.Company
strPhone = objUser.telephoneNumber
strFax = objUser.faxnumber

Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
Set objWord = CreateObject("Word.Application")
blnWeOpenedWord = True
End If
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObjects = objWord.EmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
objSelection.Style = "No Spacing"

objselection.Font.Bold = true
objSelection.TypeText UCase(strFname) & " "
if strInitial then objSelection.TypeText UCase(strInitial) & ". "
objSelection.TypeText UCase(strLname)
if strTitle then objSelection.TypeText ", " & strTitle
objSelection.TypeText Chr(11)
objselection.Font.Bold = false

if strDepartment then objSelection.TypeText strDepartment & Chr(11)
objSelection.TypeText Chr(11)
objSelection.TypeText strCompany & Chr(11)
objSelection.TypeText strStreet & Chr(11)
objSelection.TypeText strCSZ & Chr(11)
objSelection.TypeText "Phone: " & strPhone & Chr(11)
objSelection.TypeText "Fax: " & strFax & Chr(11)
objSelection.TypeText strToll & Chr(11) & Chr(11)

objSelection.TypeText strConfid
Set objSelection = objDoc.Range()

objSignatureEntries.Add "AD Signature", objSelection
objSignatureObjects.NewMessageSignature = "AD Signature"
objSignatureObjects.ReplyMessageSignature = "AD Signature"

objDoc.Close 0
If blnWeOpenedWord Then
End If


Display Name said...


This is quite a nice example, as it takes the donkey work out of generating the .htm, .rtf and .txt versions etc.

Is there a way to get the script to producde a 'webpage filtered .htm' version? This is one of the options in Word's save as, and it reduces the amount of code produced.

Display Name said...

Also, most company signatures use tables etc to get the formatting and positioning of images etc right. Looking at this script I'm not sure about how to generate a table etc for the .htm and .rtf versions, the current script I use is much more basic - writes out the html line by line, and copies the images needed from the netlogon share of the server that serviced the logon request..

Anonymous said...

How can we make the "email" and "company homepage" a hyperlink... This code is making them standard text.

Mondo said...

You should be able to just insert HTML, although I haven't tried this.
"a href='mailto:" & strEmail & "'>" & strEmail & "/a>"

Or something like that

(brackets left out because I don't know how to escape them)

Anonymous said...

I`m trying to add an existing signature "signature.html" but it doenst work , does somebody have a clue?

Regards Bart

Ben said...

To add a hyperlink I've used:

objSelection.Hyperlinks.Add objSelection.range, "http://www.web.address", , , ""

(Above code is all on one line)


Bob said...

I'm trying to use vbscript to modify a 2 signature files (.htm) and then assign one as the default for new messages and the other for replies. I have everything working in the script but I cant figure out how to make the script select each file and assign to NewMessageSignature or ReplyMessageSignature. Any help would be appreciated!

Thank you

Steve Adans said...

This has details on how to assign a default signature in Outlook from a script. There is also a downloadable script with the required code.

Hendrik Wiese said...

Just for everyone still reading this to add the email line with the hyperlink use the following:

objSelection.Hyperlinks.Add objSelection.range, "mailto:" & strEmail, , , strEmail

All in one line :)