none
VBScript - How to set the wanted file as the Outlook Email Signature

    Question

  • Hi, 

        I have pieced together a script that modifies email signature templates files (Default.htm and Return.htm) and makes it specific for the current logged on user. How do i set the Default.htm file to be used in Outlook 2007/2010 for New Messages and the Return.htm for Replies?

    Thanks,

    Thursday, July 28, 2011 6:35 PM

Answers

  • The only thing that code does is copy folders and files from the server and moves them locally to the laptop. Later in the code, i am trying to open the file, modify it, and set it as the signature file. Everything works fine except for the part where I set the file as a signature.

    *** I just figured it out the proper syntax for the code ***

    Your last statement about 'copy it as a selection' clued me in to it.

    Set objWord = CreateObject("Word.Application") 
    Set objNewSignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Default.htm")
    Set objReplySignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Return.htm")
    Set objSelectionDefault = objNewSignature.Range()
    Set objSelectionReturn = objReplySignature.Range()
    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    objSignatureEntries.Add "Default", objSelectionDefault
    objSignatureEntries.Add "Return", objSelectionReturn
    objSignatureObjects.NewMessageSignature = "Default"
    objSignatureObjects.ReplyMessageSignature = "Return"

     

    • Marked as answer by Chester64 Wednesday, August 3, 2011 2:53 PM
    Wednesday, August 3, 2011 2:53 PM

All replies

  • Search the forum with the search box at teh to of this page. Look for "OUtlook signature".  You find many examples of how to do this in a script. 

    Here is one thread that has most of what you need.

    http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/1b8512e2-6fac-4383-b012-6296281463e7

     


    jv
    Thursday, July 28, 2011 7:01 PM
  • That sets that file as both for reply and new messages. Sorry I am new to vbscripts and dont understand how to reference both files seperately.

    Thanks

    Thursday, July 28, 2011 7:10 PM
  • Di you look at the other threads.  It probaly is covered in one of them.  I can't remember what the property is off the top of my head.

    In PowerSHell yo can jsut GM the properties until you see one that looks good. You can also look them up in the Outlook help wither inOutlook or online.

     


    jv
    Thursday, July 28, 2011 8:06 PM
  • Yes I've looked in other posts. Every thead I've looked at uses the same for both new  messages and for replys. Also every thread I've looked at creates a file instead of modifing one. I'm sure it will envolve using the openObject method but i keep getting an error Type mismatch: 'OpenObject'.
    Tuesday, August 2, 2011 1:50 PM
  • Is this what you are looking for?

    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    objSignatureEntries.Add "AD Signature", objSelection
    objSignatureObjects.NewMessageSignature = "AD Signature"
    objSignatureObjects.ReplyMessageSignature = "AD Signature"


    jv
    Tuesday, August 2, 2011 3:36 PM
  • In that example you are setting 'AD Signature' to both New messages and Replies. I need to set one file to New messages and another to Replies. I am just not sure how to properly open one file set it to New message and then open another file and set it to Reply like below.

    objSignatureObjects.NewMessageSignature = "AD Signature1"

    objSignatureObjects.ReplyMessageSignature = "AD Signature2"

    Tuesday, August 2, 2011 4:50 PM
  • In that example you are setting 'AD Signature' to both New messages and Replies. I need to set one file to New messages and another to Replies. I am just not sure how to properly open one file set it to New message and then open another file and set it to Reply like below.

    objSignatureObjects.NewMessageSignature = "AD Signature1"

    objSignatureObjects.ReplyMessageSignature = "AD Signature2"

     


    Yse - so create two signatures with two different names and assign them.  I was just showing you the basics of assigning to the 'Reply' message.

     

    Same code one more step.

    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    objSignatureEntries.Add "New Message Signature", objNewSignature
    objSignatureEntries.Add "Reply Signature", objReplySignature
    objSignatureObjects.NewMessageSignature = "New Message Signature"
    objSignatureObjects.ReplyMessageSignature = "Reply Signature"
    
    
    

    This as two different signatures.  Jsut use the code linked to create your signatures and add this code to teh scipt.

     


    jv
    Tuesday, August 2, 2011 7:21 PM
  • Thank you for being patient with me, this is what i have so far

    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add()
    Set objNewSignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Default.htm")
    Set objReplySignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Return.htm")


    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    objSignatureEntries.Add "Default", objNewSignature
    objSignatureEntries.Add "Return", objReplySignature
    objSignatureObjects.NewMessageSignature = "Default"
    objSignatureObjects.ReplyMessageSignature = "Return"

    But on line 92 (the bold line) i get an error Type mismatch: 'objSignatureEntries.Add'

    any thoughts?

     

     

    Tuesday, August 2, 2011 8:19 PM
  • Her is a complete working example. It generates a complete signature. The example I postd before was just to show where the settings were located.  To make it work requires a working knowledge of scripting and teh Outlook/Word object models.

    Part of it is actually incorrect sas it is loading the signature from the same location it will be saving it to.

    I tested this so I could post it as a solution in the gallery if and when I get time to document.  It is still just a template.  You are responsible for designing the signature.  This just shows how it works.

    A signature is basivcally a copuy of teh contents of a Word document.

    ' create two signatures and add them to the Outlook signature settings.
    
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    
    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    
    objSelection.Font.Size = "10"
    objSelection.Font.Name = "arial"
    objSelection.Font.Bold = True
    objSelection.font.Color= vbRed
    objSelection.TypeText "Hello WOrld"
    
    Set objSelection = objDoc.Range()
    
    objSignatureEntries.Add "New Signature", objSelection
    objSignatureObjects.NewMessageSignature = "New Signature"
    
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    
    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    
    objSelection.Font.Size = "20"
    objSelection.Font.Name = "arial"
    objSelection.Font.Bold = True
    objSelection.font.Color= vbBlue
    objSelection.TypeText "Goodbye World"
    
    Set objSelection = objDoc.Range()
    
    objSignatureEntries.Add "Reply Signature", objSelection
    objSignatureObjects.ReplyMessageSignature = "Reply Signature"
    
    objDoc.Close 0
    objWord.Quit
    
    
    

     


    jv
    Tuesday, August 2, 2011 10:44 PM
  • Thank you for the reply, but your script is actually creating two files and then assigning them to the respective signature and in my script it modifies a set of templates (actually your idea from another post of mine that you responded to in repsonse to a formatting issue). I can post the code if you like but all i need to figure out is how to assign a file 'C:\path\default.htm' to 'objSignatureObjects.NewMessageSignature = "default" ' (or however it needs to be written).  

    Wednesday, August 3, 2011 1:05 AM
  • Thank you for the reply, but your script is actually creating two files and then assigning them to the respective signature and in my script it modifies a set of templates (actually your idea from another post of mine that you responded to in repsonse to a formatting issue). I can post the code if you like but all i need to figure out is how to assign a file 'C:\path\default.htm' to 'objSignatureObjects.NewMessageSignature = "default" ' (or however it needs to be written).  


    Chester

    The code I posted the first time is what does that.

    ' load signature file
    Set objWord = CreateObject("Word.Application")
    Set objNewSignature = objWord.Documents.Add(c:\test\default.htm")

    ' add to signatures
    objSignatureEntries.Add "Default", objNewSignature
    'Set as a signature entry
    objSignatureObjects.NewMessageSignature = "Default"


     

     

     


    jv
    Wednesday, August 3, 2011 3:54 AM
  • JV,

         I've tried that (as I mentioned a few posts again) and I receive an error messgage

                                 Error:   Type mismatch: 'objSignatureEntries.Add'

                                 Code:   800A000D

    Wednesday, August 3, 2011 1:04 PM
  • What version of Office?

    Without your code it is impossible to tell what you are doing.

     


    jv
    Wednesday, August 3, 2011 1:12 PM
  • Office 2007 and 2010. Here is the code, but it wont work right without the templates to copy over.

     

    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    
    Dim oFSO, oFolder, oFile, oTextStream, strText, objFSO, objDoc
    Set oShell = CreateObject( "WScript.Shell" )
    oAppData = oShell.ExpandEnvironmentStrings("%APPDATA%")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
      
    Set objSysInfo = CreateObject("ADSystemInfo") 
    set objFSO=CreateObject("Scripting.FileSystemObject")
    
    
     
    ' ########### This section connects to Active Directory as the currently logged on user 
     
    strUser = objSysInfo.UserName 
    Set objUser = GetObject("LDAP://" & strUser)
    
    ' ########### This section sets up the variables we want to call in the script (items on the left; whereas the items on the right are the active directory database field names) - ie strVariablename = objuser.ad.databasename 
     
    strGiven = objuser.givenName 
    strSurname = objuser.sn 
    strMobile = objuser.mobile 
    strTitle = objUser.Title 
    strPhone = objUser.telephoneNumber 
    
    set objFSO=CreateObject("Scripting.FileSystemObject")
    
      objFSO.CopyFolder "\\Server\share\*", oAppData & "\Microsoft\Signatures\"
      objFSO.CopyFile "\\Server\share\*.*", oAppData & "\Microsoft\Signatures\"
      
    Set oFolder = oFSO.GetFolder(oAppData & "\Microsoft\Signatures")
    
    For Each oFile In oFolder.Files
      If LCase(oFSO.GetExtensionName(oFile.Name)) = "htm" Then
    	Set oTextStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)
    	strText = oTextStream.ReadAll
    	oTextStream.Close
    	Set oTextStream = oFile.OpenAsTextStream(ForWriting, TristateUseDefault)
    	strText = Replace(strText, "User Name", strGiven & " " & strSurname, 1,-1,1)
    	strText = Replace(strText, "User Title", strTitle, 1,-1,1)
    	strText = Replace(strText, "UserOfficeNumber", strPhone, 1,-1,1)
    	strText = Replace(strText, "UserCellNumber", strMobile, 1,-1,1)
    	oTextStream.Write strText 
    	oTextStream.Close
    
      End If
    	Next
    	
    For Each oFile In oFolder.Files
      If LCase(oFSO.GetExtensionName(oFile.Name)) = "txt" Then
    	Set oTextStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)
    	strText = oTextStream.ReadAll
    	oTextStream.Close
    	Set oTextStream = oFile.OpenAsTextStream(ForWriting, TristateUseDefault)
    	strText = Replace(strText, "User Name", strGiven & " " & strSurname, 1,-1,1)
    	strText = Replace(strText, "User Title", strTitle, 1,-1,1)
    	strText = Replace(strText, "UserOfficeNumber", strPhone, 1,-1,1)
    	strText = Replace(strText, "UserCellNumber", strMobile, 1,-1,1)
    	oTextStream.Write strText 
    	oTextStream.Close
      End If
    	Next
    
    For Each oFile In oFolder.Files
      If LCase(oFSO.GetExtensionName(oFile.Name)) = "rtf" Then
    	Set oTextStream = oFile.OpenAsTextStream(ForReading, TristateUseDefault)
    	strText = oTextStream.ReadAll
    	oTextStream.Close
    	Set oTextStream = oFile.OpenAsTextStream(ForWriting, TristateUseDefault)
    	strText = Replace(strText, "User Name", strGiven & " " & strSurname, 1,-1,1)
    	strText = Replace(strText, "User Title", strTitle, 1,-1,1)
    	strText = Replace(strText, "UserOfficeNumber", strPhone, 1,-1,1)
    	strText = Replace(strText, "UserCellNumber", strMobile, 1,-1,1)
    	oTextStream.Write strText 
    	oTextStream.Close
      End If
    	Next
    	
    'Set objWord = OpenObject(oAppData & "\Microsoft\Signatures\Default.htm")
    
    Set objWord = CreateObject("Word.Application") 
    'Set objDoc = objWord.Documents.Add() 
    Set objNewSignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Default.htm")
    'Set objReplySignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Return.htm")
    
    
    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    objSignatureEntries.Add "Default", objNewSignature
    'objSignatureEntries.Add "Return", objReplySignature
    objSignatureObjects.NewMessageSignature = "Default"
    'objSignatureObjects.ReplyMessageSignature = "Return"
    
    'Set objEmailOptions = objWord.EmailOptions
    'Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    'Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    
    'Set objSelection = objDoc.Range() 
    'objSignatureEntries.Add "Default", objSelection
    'objSignatureObject.NewMessageSignature = "Default"
    
    'Set objDoc = OpenObject(oAppData & "\Microsoft\Signatures\Return.htm")
    'Set objSelection = objDoc.Range()
    'objSignatureEntries.Add "Return", objSelection
    'objSignatureObject.ReplyMessageSignature = "Return"

    Wednesday, August 3, 2011 1:55 PM
  • You cannot do this

      objFSO.CopyFolder "\\Server\share\*", oAppData & "\Microsoft\Signatures\"
      objFSO.CopyFile "\\Server\share\*.*", oAppData & "\Microsoft\Signatures

    You MUST open a document and copy it as a selection.  The selection can come from a template.

     

     


    jv
    Wednesday, August 3, 2011 2:10 PM
  • The only thing that code does is copy folders and files from the server and moves them locally to the laptop. Later in the code, i am trying to open the file, modify it, and set it as the signature file. Everything works fine except for the part where I set the file as a signature.

    *** I just figured it out the proper syntax for the code ***

    Your last statement about 'copy it as a selection' clued me in to it.

    Set objWord = CreateObject("Word.Application") 
    Set objNewSignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Default.htm")
    Set objReplySignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Return.htm")
    Set objSelectionDefault = objNewSignature.Range()
    Set objSelectionReturn = objReplySignature.Range()
    Set objEmailOptions = objWord.EmailOptions
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    objSignatureEntries.Add "Default", objSelectionDefault
    objSignatureEntries.Add "Return", objSelectionReturn
    objSignatureObjects.NewMessageSignature = "Default"
    objSignatureObjects.ReplyMessageSignature = "Return"

     

    • Marked as answer by Chester64 Wednesday, August 3, 2011 2:53 PM
    Wednesday, August 3, 2011 2:53 PM
  • The only thing that code does is copy folders and files from the server and moves them locally to the laptop. Later in the code, i am trying to open the file, modify it, and set it as the signature file. Everything works fine except for the part where I set the file as a signature.

    *** I just figured it out the proper syntax for the code ***

    Your last statement about 'copy it as a selection' clued me in to it.

    Set objWord = CreateObject("Word.Application") 
    
    Set objNewSignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Default.htm")
    
    Set objReplySignature = objWord.Documents.Add(oAppData & "\Microsoft\Signatures\Return.htm")
    
    Set objSelectionDefault = objNewSignature.Range()
    
    Set objSelectionReturn = objReplySignature.Range()
    
    Set objEmailOptions = objWord.EmailOptions
    
    Set objSignatureObjects = objWord.EmailOptions.EmailSignature
    
    Set objSignatureEntries = objSignatureObjects.EmailSignatureEntries
    
    objSignatureEntries.Add "Default", objSelectionDefault
    
    objSignatureEntries.Add "Return", objSelectionReturn
    
    objSignatureObjects.NewMessageSignature = "Default"
    
    objSignatureObjects.ReplyMessageSignature = "Return"

     

    Yup! _ was wondering how long it would take,  I ran out of ways to say it.

    You cannot use a signature file- it must be a selection of a loaded documrnt.  I went through the same thing until my slow brain finally caught the mesage.

    I am joping to have some time to put together a more complrehensive demo project and place it in the repository.  This question comes up far too often.

    I am surprised that there is not a Group Policy to distribute signatures based on a corporate standard.

     


    jv
    Wednesday, August 3, 2011 3:23 PM