none
Script VBS to create a new signature for outlook RRS feed

  • Question

  • Hi folks,

    I made a script to create a new signature on Outlook.
    Note: I'm not a expert in scripts but i'm doing my best to do it.

    When I execute manually the script, everything works fine.

    When I execute the script by PsExec or SCCM or GPO has something wrong that I don't know how to identify and fix it.

    When I execute the script manually the script enables for "New Messages" and "Replying/Forward"  and if I open the new email the signature shows up automatically.

    When I execute the script by GPO / SCCM or PsExec the script works but when I click on the button "New Email" the signature does not show up automatically. I need to click on the button "Signature" and enable the signature. If i want to put the script to open automatially i need to change the "New Messages" and "Replying/Forward".

    I don't want to change this option manually for everyone. Is there any way to fix it?

    Thank you and I'm sorry for this issue.

    There's the script (I know is not good, as i told above, i'm a not good scripter)

    '##########################################
    '########## Automated Signature ###########
    '########## Version 2.0         ###########
    '########## By Julio Melo      ############
    '##########################################
    'V2, add a condition to vertify the mobile phone
    'If the user has the mobile it will show up
    'if the user has not the mobile phone won't show up

    On Error Resume Next
    'connecting to the AD
    Set objSysInfo = CreateObject("ADSystemInfo")
    'User AD to coonect LDAP
    strUser = objSysInfo.UserName
    Set objUser = GetObject("LDAP://" & strUser)
    'Setting the variables
    strField = objUser.mobile
    strName = objUser.FullName
    strTitle = objUser.Title
    strAddress = objUser.streetaddress
    strPhone = objUser.telephoneNumber
    strMobile = objUser.mobile
    strFax = objUser.facsimileTelephoneNumber
    strEmail = objUser.mail

    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
    'condition to verify that Mobile phone is empty
    ' if the mobile phone is empety won't show any mobile info
    If strField & "" = "" Then
        'This is Null value
    objSelection.ParagraphFormat.LineSpacing = 10
    objSelection.Font.Size = "11" 
    objSelection.Font.Name = "Arial" 
    objSelection.Font.Bold = False 
    objSelection.TypeText strName & ""
    objSelection.Font.Size = "14" 
    objSelection.Font.Name = "Arial" 
    objSelection.Font.Bold = True 
    objSelection.TypeParagraph()
    objSelection.Font.Color = RGB(2,81,147)
    objSelection.TypeText strTitle
    objSelection.Font.Color = RGB(39,36,36) 
    objSelection.TypeParagraph() 
    objSelection.ParagraphFormat.LineSpacing = 12
    objSelection.InlineShapes.AddPicture("\\SERVERX\Sources\Scripts\VBS\signature\logo.png")
    objSelection.TypeParagraph()
    objSelection.ParagraphFormat.LineSpacing = 9 
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True 
    objSelection.TypeText "Address:    "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strAddress
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True 
    objSelection.TypeText "Tel:            "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strPhone
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True   
    objSelection.Font.Bold = False
    objSelection.TypeText strMobile 
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True   
    objSelection.TypeText "Fax:           "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strFax
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True  
    objSelection.TypeText "Email:         "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.Font.Color = RGB(2,81,147)
    objSelection.TypeText strEmail
    objSelection.TypeParagraph()
    objSelection.Font.Size = "12" 
    objSelection.Font.Name = "Arial"
    objSelection.TypeText "                       "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"http://www.xyz.com",,"Address", "www.celtrino.com")
    objSelection.TypeParagraph()
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://www.linkedin.com/company/xyz",,"Our LinkedIn", "LinkedIn")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://twitter.com/xyz",,"Follow Us", "Twitter")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://www.youtube.com/user/xyz",,"Subscribe Us", "YouTube")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://plus.google.com/+xyz",,"Add Us", "Google+")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://www.facebook.com/xyz/",,"Be Social", "Facebook")

    Set objSelection = objDoc.Range()

    objSignatureEntries.Add "XY AD Signature", objSelection
    objSignatureObject.NewMessageSignature = "XY AD Signature"
    objSignatureObject.ReplyMessageSignature = "XY AD Signature"
    objDoc.Saved = True
    objWord.Quit

    Else
        'This is NOT Null value
    objSelection.ParagraphFormat.LineSpacing = 10
    objSelection.Font.Size = "11" 
    objSelection.Font.Name = "Arial" 
    objSelection.Font.Bold = False 
    objSelection.TypeText strName & ""
    objSelection.Font.Size = "14" 
    objSelection.Font.Name = "Arial" 
    objSelection.Font.Bold = True 
    objSelection.TypeParagraph()
    objSelection.Font.Color = RGB(2,81,147)
    objSelection.TypeText strTitle
    objSelection.Font.Color = RGB(39,36,36) 
    objSelection.TypeParagraph() 
    objSelection.ParagraphFormat.LineSpacing = 12
    objSelection.InlineShapes.AddPicture("\\SERVERX\Sources\Scripts\VBS\signature\logo.png")
    objSelection.TypeParagraph()
    objSelection.ParagraphFormat.LineSpacing = 9 
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True 
    objSelection.TypeText "Address:    "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strAddress
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True 
    objSelection.TypeText "Tel:            "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strPhone
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True   
    objSelection.TypeText "Mob:          " 
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strMobile 
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True   
    objSelection.TypeText "Fax:           "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.TypeText strFax
    objSelection.TypeParagraph()
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = True  
    objSelection.TypeText "Email:         "
    objSelection.Font.Size = "10" 
    objSelection.Font.Name = "Tahoma" 
    objSelection.Font.Bold = False
    objSelection.Font.Color = RGB(2,81,147)
    objSelection.TypeText strEmail
    objSelection.TypeParagraph()
    objSelection.Font.Size = "12" 
    objSelection.Font.Name = "Arial"
    objSelection.TypeText "                       "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"http://www.xyz.com",,"Address", "www.celtrino.com")
    objSelection.TypeParagraph()
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://www.linkedin.com/company/xyz",,"Our LinkedIn", "LinkedIn")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://twitter.com/xyz",,"Follow Us", "Twitter")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://www.youtube.com/user/xyz",,"Subscribe Us", "YouTube")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://plus.google.com/+Celtrino",,"Add Us", "Google+")
    objSelection.TypeText " | "
    objLink = objSelection.Hyperlinks.Add(objSelection.Range,"https://www.facebook.com/xyz/",,"Be Social", "Facebook")

    Set objSelection = objDoc.Range()

    objSignatureEntries.Add "Celtrino AD Signature", objSelection
    objSignatureObject.NewMessageSignature = "Celtrino AD Signature"
    objSignatureObject.ReplyMessageSignature = "Celtrino AD Signature"
    objDoc.Saved = True
    objWord.Quit

    End If


    Na vida há dois caminhos mais só um que vai na direção certa

    Wednesday, February 1, 2017 4:50 PM

Answers

  • There are numerous errors and mistakes in you script.  Don't ever use "On Error Resume Next" unless you plane to check each startemnt for an error.  Once that was removed most of the errors became obvious.

    Here is how to generate a signature the easy way.

    strTEmplate = "\\server\documents\sig1.dotx"
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add(strTemplate)
    
    Set objSysInfo = CreateObject("ADSystemInfo")
    strUser = objSysInfo.UserName
    Set objUser = GetObject("LDAP://" & strUser)
    With objDoc.Bookmarks
    	.Item("FullName").Range.Text = objUser.FullName
    	.Item("Title").Range.Text = objUser.Title
    	.Item("streetaddress").Range.Text = objUser.streetaddress
    	.Item("telephoneNumber").Range.Text = objUser.telephoneNumber
    	If objUser.mobile.Length <> 0  Then
    		.Item("Mobile").Range.Text = objUser.mobile
    	Else
    		.Item("Mobile").Range.Delete()
    	End If
    	.Item("facsimileTelephoneNumber").Range.Text = objUser.facsimileTelephoneNumber
    	.Item("mail").Range.Text = objUser.mail
    End With
    
    Set selection = objDoc.Range()
    With objWord.EmailOptions.EmailSignature
    	.EmailSignatureEntries.Add "Celtrino AD Signature", selection
    	.NewMessageSignature = "Celtrino AD Signature"
    	.ReplyMessageSignature = "Celtrino AD Signature"
    End With
    
    objDoc.Saved = True
    objWord.Quit

    This loads a template from the network that has the format, images and links you want.  It then just assigns the bookmarks by name and quits.

    This version should work easily in a logon script.   You should create a way to prevent it from running every time the user logs on.  The easy way to do that is to check the time on the template file and save it in the registry. If the time is newer then regenerate the signature.

    If you want I can give you a link to the template that I used for testing.  I created it from your original code nut even the Word doc created had numerous issues and would not work well in various word versions.


    \_(ツ)_/




    Thursday, February 2, 2017 4:20 AM

All replies

  • You cannot run Word under any of those subsystems.  Word must be run under a user context and a full UI.

    YOU can run it under a login script if you run the scripts synchronously and after network availability.


    \_(ツ)_/

    Wednesday, February 1, 2017 6:01 PM
  • There are numerous errors and mistakes in you script.  Don't ever use "On Error Resume Next" unless you plane to check each startemnt for an error.  Once that was removed most of the errors became obvious.

    Here is how to generate a signature the easy way.

    strTEmplate = "\\server\documents\sig1.dotx"
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add(strTemplate)
    
    Set objSysInfo = CreateObject("ADSystemInfo")
    strUser = objSysInfo.UserName
    Set objUser = GetObject("LDAP://" & strUser)
    With objDoc.Bookmarks
    	.Item("FullName").Range.Text = objUser.FullName
    	.Item("Title").Range.Text = objUser.Title
    	.Item("streetaddress").Range.Text = objUser.streetaddress
    	.Item("telephoneNumber").Range.Text = objUser.telephoneNumber
    	If objUser.mobile.Length <> 0  Then
    		.Item("Mobile").Range.Text = objUser.mobile
    	Else
    		.Item("Mobile").Range.Delete()
    	End If
    	.Item("facsimileTelephoneNumber").Range.Text = objUser.facsimileTelephoneNumber
    	.Item("mail").Range.Text = objUser.mail
    End With
    
    Set selection = objDoc.Range()
    With objWord.EmailOptions.EmailSignature
    	.EmailSignatureEntries.Add "Celtrino AD Signature", selection
    	.NewMessageSignature = "Celtrino AD Signature"
    	.ReplyMessageSignature = "Celtrino AD Signature"
    End With
    
    objDoc.Saved = True
    objWord.Quit

    This loads a template from the network that has the format, images and links you want.  It then just assigns the bookmarks by name and quits.

    This version should work easily in a logon script.   You should create a way to prevent it from running every time the user logs on.  The easy way to do that is to check the time on the template file and save it in the registry. If the time is newer then regenerate the signature.

    If you want I can give you a link to the template that I used for testing.  I created it from your original code nut even the Word doc created had numerous issues and would not work well in various word versions.


    \_(ツ)_/




    Thursday, February 2, 2017 4:20 AM
  • Mate,

    First of all, Thank you!

    I'm sorry for my mistakes, I have knowledge about infrastructure, i'm not a good developer.

    I tried to run this script i got the error on line 1.

    I did not understand that variable 

    bTesting = True


    Na vida há dois caminhos mais só um que vai na direção certa

    Thursday, February 2, 2017 10:03 AM
  • delete that line.  It is left over from testing.

    When posting please post complete error message.


    \_(ツ)_/


    • Edited by jrv Thursday, February 2, 2017 6:53 PM
    Thursday, February 2, 2017 6:51 PM
  • Ok.

    Thank you, next time i'll post the error.


    Na vida há dois caminhos mais só um que vai na direção certa

    Friday, February 3, 2017 11:37 AM
  • There are numerous errors and mistakes in you script.  Don't ever use "On Error Resume Next" unless you plane to check each startemnt for an error.  Once that was removed most of the errors became obvious.

    Here is how to generate a signature the easy way.

    strTEmplate = "\\server\documents\sig1.dotx"
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add(strTemplate)
    
    Set objSysInfo = CreateObject("ADSystemInfo")
    strUser = objSysInfo.UserName
    Set objUser = GetObject("LDAP://" & strUser)
    With objDoc.Bookmarks
    	.Item("FullName").Range.Text = objUser.FullName
    	.Item("Title").Range.Text = objUser.Title
    	.Item("streetaddress").Range.Text = objUser.streetaddress
    	.Item("telephoneNumber").Range.Text = objUser.telephoneNumber
    	If objUser.mobile.Length <> 0  Then
    		.Item("Mobile").Range.Text = objUser.mobile
    	Else
    		.Item("Mobile").Range.Delete()
    	End If
    	.Item("facsimileTelephoneNumber").Range.Text = objUser.facsimileTelephoneNumber
    	.Item("mail").Range.Text = objUser.mail
    End With
    
    Set selection = objDoc.Range()
    With objWord.EmailOptions.EmailSignature
    	.EmailSignatureEntries.Add "Celtrino AD Signature", selection
    	.NewMessageSignature = "Celtrino AD Signature"
    	.ReplyMessageSignature = "Celtrino AD Signature"
    End With
    
    objDoc.Saved = True
    objWord.Quit

    This loads a template from the network that has the format, images and links you want.  It then just assigns the bookmarks by name and quits.

    This version should work easily in a logon script.   You should create a way to prevent it from running every time the user logs on.  The easy way to do that is to check the time on the template file and save it in the registry. If the time is newer then regenerate the signature.

    If you want I can give you a link to the template that I used for testing.  I created it from your original code nut even the Word doc created had numerous issues and would not work well in various word versions.


    \_(ツ)_/




    hello, i'm interested by your word template.

    can you share it with me ?

    Wednesday, September 4, 2019 4:16 PM
  • hello, i'm interested by your word template.

    can you share it with me ?

    You have to create your own template and add bookmarks you need to it.  This requires that you know Word.  Post in the Word forum for help on creating templates with named bookmarks.

    This thread is years old and you need to open your own topic for further help.  I also recommend using PowerShell as it is more powerful and easier to use since it can be tested line by line interactively.


    \_(ツ)_/

    Wednesday, September 4, 2019 4:21 PM