Active Directory Outlook Signature – VBScript

Update: This article was updated on September 10, 2020.

See also a more recent article on how to create a HTML email signature in Outlook using VBScript.

Setting up an email signature for each Outlook user in the organization can be a time-consuming job, especially when performed manually. Moreover – every time a change in the signature is required the whole process needs to be repeated.

That is why system administrators are searching the web for a more centralized way of performing this task. One of the possible solutions is to run a logon script, distributed to workstations via the Group Policy Object. When the end user logs on, the script gathers information about that particular person from Active Directory and creates a personalized email signature in Outlook. Below, you will find a guide on how to configure such a script.

Outlook Signatures VBScript

Script preparation

Firstly you need a VBScript that creates a signature in Outlook. Below you can find an example code, that reads user’s information from Active Directory. That information is then inserted into the signature. Lastly, the script sets the signature to be added to new emails and reply emails.

On Error Resume Next

Set objSysInfo = CreateObject("ADInformation")

strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
If objUser Is Empty Then _
MsgBox "No connection with LDAP information.", _
vbCritical, "Error": Exit Sub

strName = objUser.FullName
strTitle = objUser.Title
strDepartment = objUser.Department
strCompany = objUser.Company
strPhone = objUser.telephoneNumber

Set objWord = CreateObject("Word.Application")

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature

Set objSignatureEntries = objSignatureObject.EmailSignatureEntries

' Beginning of signature block

objSelection.TypeText strName & ", " & strTitle
objSelection.TypeParagraph()
objSelection.TypeText strDepartment
objSelection.TypeParagraph()
objSelection.TypeText strCompany
objSelection.TypeParagraph()
objSelection.TypeText strPhone

' End of signature block

Set objSelection = objDoc.Range()

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

objDoc.Saved = True
objWord.Quit

Simply copy the above text into any plain text editor (e.g. Windows Notepad) and save it with the VBS file extension.

To test the script execute it on a workstation with Outlook installed (make sure that Outlook is not running). Next time you launch Outlook and create a new message it should contain a signature, similar to the example below:

2vb

To modify the signature change the code between ‘ Beginning of signature block and ‘ End of signature block in the script. Let’s say you need to add user’s email address at the very end of the signature. You can do this by adding the following line in the signature block of the script:

objSelection.TypeParagraph()
objSelection.TypeText strEmail

The resulting signature block in the script is as follows:

' Beginning of signature block

objSelection.TypeText strName & ", " & strTitle
objSelection.TypeParagraph()
objSelection.TypeText strDepartment
objSelection.TypeParagraph()
objSelection.TypeText strCompany
objSelection.TypeParagraph()
objSelection.TypeText strPhone
objSelection.TypeParagraph()
objSelection.TypeText strEmail

' End of signature block

To enter any string of text that is not dynamically gathered from Active Directory, simply replace the placeholder part of the objSelection.TypeText command (e.g. strEmail) with a text placed between the quotation marks. In the example below the environmental disclaimer is added:

' Beginning of signature block

objSelection.TypeText strName & ", " & strTitle
objSelection.TypeParagraph()
objSelection.TypeText strDepartment
objSelection.TypeParagraph()
objSelection.TypeText strCompany
objSelection.TypeParagraph()
objSelection.TypeText strPhone
objSelection.TypeParagraph()
objSelection.TypeText strEmail
objSelection.TypeParagraph()
objSelection.TypeText "Please consider the environment before printing this message."

' End of signature block

Adding images and advanced formatting

Email signatures work best when they are formatted in tables. This way, they should keep their formatting regardless of your recipients’ email clients or devices.

However, advanced formatting as well as images make it trickier to create a working script file. The sample script presented below adds a company logo and social media buttons to the signature.

On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")

strUser = objSysInfo.UserName

Set objUser = GetObject("LDAP://" & strUser)

If IsEmpty(objUser) Then

MsgBox "No connection with LDAP information.", vbCritical, "Error": WScript.Quit (1)

strName = objUser.FullName

strTitle = objUser.Title

strDepartment = objUser.Department

strCompany = objUser.Company

strPhone = objUser.telephoneNumber

'location of logo and social media images. Mind to use links to a network share, to which users will have access.

strLogo = 'logo image location

strSoc1 = '1. social media button location

strSoc2 = '2. social media button location

strSoc3 = '3. social media button location

strSoc4 = '4. social media button location

strFBlink = '1. link to social media website

strYTlink = '2. link to social media website

strTWlink = '3. link to social media website

strLNlink = '4. link to social media website

Set objWord = CreateObject("Word.Application")

Set objDoc = objWord.Documents.Add()

Set objSelection = objWord.Selection

Set objEmailOptions = objWord.EmailOptions

Set objSignatureObject = objEmailOptions.EmailSignature

Set objSignatureEntries = objSignatureObject.EmailSignatureEntries

' Beginning of signature block

Set objRange = objDoc.Range()

'Create table for signature content

objDoc.Tables.Add objRange,6,5

Set objTable = objDoc.Tables(1)

objdoc.Paragraphs.SpaceAfter = 0

'Merge selected table cells

objTable.Cell(1,1).Merge objTable.Cell(5,1)

objTable.Cell(1,2).Merge objTable.Cell(1,5)

objTable.Cell(2,2).Merge objTable.Cell(2,5)

objTable.Cell(3,2).Merge objTable.Cell(3,5)

objTable.Cell(4,2).Merge objTable.Cell(4,5)

objTable.Cell(5,2).Merge objTable.Cell(5,5)

'Add the company logo

objTable.Cell(1,1).Range.InlineShapes.AddPicture(strLogo)

'Add user's data

objTable.Cell(1,2).Range.Font.Name = "Calibri"

objTable.Cell(1,2).Range.Font.Size = "10"

objTable.Cell(1,2).Range.Font.Bold = True

objTable.Cell(1,2).Range.Text = strName & ", " & strTitle

objTable.Cell(2,2).Range.Font.Name = "Calibri"

objTable.Cell(2,2).Range.Font.Size = "10"

objTable.Cell(2,2).Range.Font.Bold = False

objTable.Cell(2,2).Range.Text = strDepartment

objTable.Cell(3,2).Range.Font.Name = "Calibri"

objTable.Cell(3,2).Range.Font.Size = "10"

objTable.Cell(3,2).Range.Font.Bold = False

objTable.Cell(3,2).Range.Text = strCompany

objTable.Cell(4,2).Range.Font.Name = "Calibri"

objTable.Cell(4,2).Range.Font.Size = "10"

objTable.Cell(4,2).Range.Font.Bold = False

objTable.Cell(4,2).Range.Text = "P: " & strPhone

objTable.Cell(5,2).Range.Font.Name = "Calibri"

objTable.Cell(5,2).Range.Font.Size = "10"

objTable.Cell(5,2).Range.Font.Bold = False

objTable.Cell(5,2).Range.Text = "www.my-company.com"

'Add social media buttons

objtable.Cell(6,2).width = 24

objtable.Cell(6,2).height = 24

objTable.Cell(6,2).Range.InlineShapes.AddPicture(strSoc1)

objDoc.Hyperlinks.Add objDoc.InlineShapes.Item(2), strFBlink

objtable.Cell(6,3).width = 24

objtable.Cell(6,3).height = 24

objTable.Cell(6,3).Range.InlineShapes.AddPicture(strSoc2)

objDoc.Hyperlinks.Add objDoc.InlineShapes.Item(3), strYTlink

objtable.Cell(6,4).width = 24

objtable.Cell(6,4).height = 24

objTable.Cell(6,4).Range.InlineShapes.AddPicture(strSoc3)

objDoc.Hyperlinks.Add objDoc.InlineShapes.Item(4), strTWlink

objtable.Cell(6,5).width = 24

objtable.Cell(6,5).height = 24

objTable.Cell(6,5).Range.InlineShapes.AddPicture(strSoc4)

objDoc.Hyperlinks.Add objDoc.InlineShapes.Item(5), strLNlink

' End of signature block

Set objSelection = objDoc.Range()

objSignatureEntries.Add "AD Signature", objSelection

'objSignatureObject.NewMessageSignature = "AD Signature"

'objSignatureObject.ReplyMessageSignature = "AD Signature"

objDoc.Saved = True

objWord.Quit

Running the script results in the following signature:

VBS old script upgrade

Distributing the signature via GPO

To distribute the script automatically to all workstations in the organization, the easiest way is to use a GPO policy. This method will provide every single user with the signature when they log on.

To begin log on to your server and click the Start menu button, then select Administrative Tools menu and click Group Policy Management. Alternatively you can run the gpmc.msc command in the Start menu search field. This opens the main GPO console window:

1vb

In the left pane expand the branch with your server name, then expand Group Policy Objects. Next right-click the Default Domain Policy object select Edit.

3vb

In the Group Policy Management Editor navigate to User Configuration in the left pane, then go to: Policies, Windows Settings, Scripts (Logon/Logoff).

4vb

Next, double click the Logon option visible in the right pane to launch the Logon Properties window. Then click the Show Files… button to open the folder containing scripts. Copy your VBScript into that folder. Next, in the Logon Properties window click the Add… button and select the script file that you just copied in by clicking the Browse… button.

5vb

Save the settings by clicking the OK button all the way down.

That’s it – next time your users log on to their workstations, the script will launch and create the Outlook signature that contains user’s AD information.

This method is fairly useful. However, it has some drawbacks:

  • Users can modify their signatures before sending which might breach the unified signature look policy in your company.
  • Every change in the signature requires all users to log off and log back on to re-run the script and apply changes.
  • Signature editing is quite complicated and requires a bit of programming knowledge.
  • Administrative access to the server is required.

Fortunately there are more user friendly solutions, such as CodeTwo Exchange Rules and CodeTwo Exchange Rules Pro (for the on-premises Exchange Server) or CodeTwo Email Signatures for Office 365 (for Microsoft 365 environments). Not only do they overcome all above limitations, but also deliver much more features:

  • Easy to use GUI that lets you create any kind of signature in a friendly way;
  • A signature testing facility allows to check and see how your signature template looks like before deploying it to the live environment;
  • It supports all emails processed by the Exchange transport service, no matter from what email client they were sent by the end user;
  • The program provides a remote access to the signature deployment. This way the process can be delegated to e.g. your marketing team without giving them direct access to the server;
  • A rich library of ready to use signature templates is available;
  • All email formats are supported – no matter it’s HTML, Plain Text, or RTF;
  • You can download and use the fully functional program for 30 days (or 14 days in the case of CodeTwo Email Signatures for Office 365) to see if it suit your needs.
  • And more.
CodeTwo Products

28 thoughts on “Active Directory Outlook Signature – VBScript


  1. Is Exchange required for this to work? I tried on a local setup and after working on the suggested script getting no errors I tried it from the workstation and the server and did not get any signature. Am at a loss here. strName and strTitle where are these values from are they the users info in Group Policys??

    • In the current form of the script, it is not possible to add graphics or background colors. However, you can use transport rules to add an HTML email signature (with colors and graphics). Here is a guide on how to do that: Email Signatures on Exchange

    • Hello Eddie,
      The number should not have any influence on if and how the script works.

  2. Hallo Adam,
    I got also “No connection with LDAP information” (Windows 10.1607)
    Your comment is that this is environment related.
    But whatever could this be?

    Kind Regards
    Ronald

    • Hello Ronald,
      I have just tested this script on a Windows 10 machine and I get the error as well. The issue is incredibly weird, as when I delete the If statement:
      If objUser Is Empty Then _
      MsgBox "No connection with LDAP information.", _
      vbCritical, "Error": Exit Sub

      The signature appears without any problems, which means that the objUser is not empty.
      Please try deleting the if statement to see if the script adds the signature correctly. If not – it means that the machine cannot connect with the LDAP correctly.

  3. Hi, I have the same issue as Andreas. All I get is a comma. When I add “If objUser Is Empty Then Exit Sub” I have this error message : “Instruction ‘exit’ incorrecte’.
    Can you help me please ?

    • I have not tested the script on Outlook 2013, but it should work. The problem is that, as it gets users’ data from the on-premises Active Directory, it will not be able to get any information from Office 365.
      If you want to create automatic email signatures in Office 365, you might be better off creating a mail flow rule. You can see how to do this in this article: How to create company-wide email signatures and disclaimers in Office 365.

  4. Hi Adam
    Does the gpo method work for mac outlook 2016 (we were able to do it to mac outlook 2011), as we are having issue creating the default html signature for each user on mac 2016, as the mac outlook 2016 have move the signature location on the local drive.

    • Unfortunately, I am afraid you would need a third party tool to solve the problem. Have you considered adding email signatures on the server level? They would work on emails sent from any email client.

  5. Hi

    Thank you for the code and explanation.

    I have tried this however getting an error ‘invalid exit statement’ on line 9.

    Can you please assist

    Thank you

    • Hi Ali,

      Just substitute the ‘Please consider the environment before printing this message.’ part with whatever you want your disclaimer to say.

      Hope this helps,
      Adam

  6. I you do not have strPhone or strMobile for example, you should use IF() finction.

    If Len(Trim(strPhone)) > 0 Then strPhone = “Ph. ” & strPhone & ” | ”
    If Len(Trim(strMobile)) > 0 Then strMobile = “Mob. ” & strMobile & ” | ”
    objSelection.TypeText strPhone & strMobile

  7. Hi Adam,
    Thanks for the script. It works for me. I am very new in scripting and would like to know how to insert a line if the AD field is null. For example if a user does not have a mobile number. Please see my script below.

    On Error Resume Next

    Set objSysInfo = CreateObject(“ADSystemInfo”)

    strUser = objSysInfo.UserName
    Set objUser = GetObject(“LDAP://” & strUser)

    strName = objUser.FullName
    strTitle = objUser.Title
    strCompany = objUser.Company
    strPhone = objUser.telephoneNumber
    strMobile = objUser.Mobile

    Set objWord = CreateObject(“Word.Application”)

    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection

    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObject = objEmailOptions.EmailSignature

    Set objSignatureEntries = objSignatureObject.EmailSignatureEntries

    objSelection.Style = “No Spacing”
    ‘objSelection.Font.Name = “Tahoma”
    ‘objSelection.Font.Size = “14”
    objSelection.TypeText strName & vbLF
    objSelection.Font.Color = RGB(190,190,190)
    objSelection.TypeText strTitle & ” | ”
    objSelection.Font.Color = RGB(0,0,0)
    objSelection.Font.Underline = True
    objSelection.Font.Bold = true
    objselection.TypeText strCompany
    objSelection.TypeParagraph()
    objSelection.Font.Underline = false
    objSelection.Font.Bold = false
    objSelection.TypeText “Ph. ” & strPhone & ” | ” & “Mob. ” & strMobile & ” | ”

    Set objSelection = objDoc.Range()

    objSignatureEntries.Add “AD Signature”, objSelection
    objSignatureObject.NewMessageSignature = “AD Signature”
    objSignatureObject.ReplyMessageSignature = “AD Signature”

    objDoc.Saved = True
    objWord.Quit

  8. @Andreas, you’ve get single comma, because you have no connections with LDAP.
    Your signature use strName & “, ” & strTitle when strName and strTitle is a empty string (as the rest of it).

    You can add this linle after Set objUsr …
    If objUser Is Empty Then Exit Sub

  9. Hi,

    I tried your script example and it is added in outlook signatures.
    Now all I get is a comma. What do you think is wrong here?

    Thanks,
    Andreas

  10. Hi Adam

    Quick question:

    I tried using your script and it pretty much works for me. But I don’t want the paragraphs so I deleted them. Afterwards I executed the vbs again but the paragraphs are still there..

    I can’t fix this with either vbCrLf nor vbCR or vbLf..

    Do you have an idea?

    Kind regards
    Marcel

    • Hi Marcel,

      Sorry for the late reply.

      Do I understand correctly that instead of paragraphs you want single line breaks (such as you would get by pressing shift+enter)? If so, please try using this code:

      ' Beginning of signature block

      objSelection.TypeText strName & ", " & strTitle & Chr(11)
      objSelection.TypeText strDepartment & Chr(11)
      objSelection.TypeText strCompany & Chr(11)
      objSelection.TypeText strPhone

      ' End of signature block

      If I misunderstood your intention, please post an image of what you would like your signature to look like, or try to convey it in the comments.

      Thank you!
      Adam

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

*