locked
Trying to standardize email signatures using .OpenTextFile and .CreateTextFile RRS feed

  • Question

  • I am attempting to standardize signature files throughout the company by taking an existing signature and changing it to incorporate active directory information.  I finished the html file with no issues - works great - but the text file portion breaks.  Can someone please help me figure out what is wrong with the below code? 

    I've tried changing some of the lines to figure out where the problem lies and it appears to fail either when it's opening the .txt file or when it tries to read the contents of that file into a variable.  I also tried hard coding the path and that didn't help.  The code is identical to the .htm version which works great so I don't know why the .txt version is failing.  Any help would be much
    appreciated

    By the way - some of the formatting and spacing got messed up in the copy/paste - I'll try to fix it but hopefully the terrible line-wrapping doesn't mess up the review.
    ----------------------
    'Option Explicit
    On Error Resume Next
       Dim filesys
       Dim strSigName, strFileTextOld, StrFileTextNew, strSigBlockOld, strSigBlockNew
       Dim strHTMFileDest, strRTFFileDest, strTXTFileDest, strSigFolder
       Dim strHTMFileOrig, strRTFFileOrig, strTXTFileOrig
       Dim strFullName, strTitle, strCompany, strCity, strTel, strCell, strFax, strWeb, strWebD, strDisc1, strDisc2
       Dim boolUpdateStyle

    '==========================================================================

    ' This script takes an existing signature and modifies it to include the company signature block
    ' This script requires the following in order to be successful:
    '    1.  Build a signature in Outlook with all the pictures/etc. laid out in the format this is desired
    '    2.  Place a unique string of text in the signature that can be read and replaced.  In this version, we use "XXXXXXXXXX" in all caps.  Put that text into the strSigBlockOld variable below

    '    3.  Make a note of the name and put that in the strSigName variable below.  In this version, we use Default (New)
    '    4.  Copy all files associated with that signature into the same directory as the vbs executable.  These will be copied
    to the user's Signature folder

    '==========================================================================


    '==========================================================================
    ' Some script variables
    '==========================================================================

       strSigName  = "Default (New)"
       strSigBlockOld = "XXXXXXXXXX"
    '  If signature exists, overwrite (true) or leave alone (false)?
       boolUpdateStyle = true

    '==========================================================================
    ' Read User's Active Directory information
    '==========================================================================

       Dim objSysInfo, objUser
       Set objSysInfo = CreateObject("ADSystemInfo")
       Set objUser    = GetObject("LDAP://" & objSysInfo.Username)
       strFullName = objUser.displayname
       strTitle    = objUser.title
       strCompany = "Company"
       strCity = objuser.l 
       strCell  = objuser.mobile
       strWeb      = "h_ttp:URL" (had to change these in order to post)
       strWebD   = "url" (had to change these in order to post)

       strTel = "111-111-1111"
       strFax = "111-111-1112"

       If Trim(strTitle) = "" Then strTitle = "_"
       Set objUser    = Nothing 
       Set objSysInfo = Nothing


    '==========================================================================
    ' Get Signature Folder
    '==========================================================================

       Dim objShell
       Set objShell = CreateObject("WScript.Shell")

       strSigFolder = ObjShell.ExpandEnvironmentStrings("%appdata%") & "\Microsoft\Signatures\"
       Set objShell = Nothing


    '==========================================================================
    ' Copy Files to Destination and Set Signature File Variables
    '==========================================================================

       Dim objFSO, objFile
       Set objFSO   = CreateObject("Scripting.FileSystemObject")

    ' Create signature folder if it doesn't exist
       If Not (objFSO.FolderExists(strSigFolder)) Then
          Call objFSO.CreateFolder(strSigFolder)
       End If
     'set variables for original files
       strHTMFileOrig = strSigName & ".htm"
       strRTFFileOrig = strSigName & ".rtf"
       strTXTFileOrig = strSigName & ".txt"
       strSigFilesOrig = strSigName & "_files"

    ' set variables for destination files
       strHTMFileDest = strSigFolder & strSigName & ".htm"
       strRTFFileDest = strSigFolder & strSigName & ".rtf"
       strTXTFileDest = strSigFolder & strSigName & ".txt"

    ' copy folder for html stuff
       Set filesys=CreateObject("Scripting.FileSystemObject")
       objFSO.CopyFolder strSigFilesOrig, strSigFolder

    '==========================================================================
    ' Modify HTM File and create at destination
    '==========================================================================
    'chr(47) = /
    ' Build Signature block (check for Cell Phone)
       If strCell <> "" then 

         strSigBlockNew = strFullName & "<br/>"&vbCrLf & strTitle & "<br />"&vbCrLf & strCompany & "<br />"&vbCrLf & "<br />"&vbCrLf & strCity & "<br />"&vbCrLf & strTel& "<br/>"&vbCrLf & strCell & " Cell <br />"&vbCrLf & strFax & " Fax <br />"&vbCrLf & "<a href=""" &strWeb & """ style=""FONT-SIZE: 12pt; COLOR:#000080; FONT-FAMILY: Garamond""" &
    ">" & strWebD & "<" & Chr(47) & "a>"&bCrLf & "<br />"&vbCrLf
       else
          strSigBlockNew = strFullName & "<br />"&vbCrLf & strTitle & "<br />"&vbCrLf & strCompany & "<br />"&vbCrLf & "<br />"&vbCrLf & strCity & "<br />"&vbCrLf & strTel& "<br/>"&vbCrLf & strFax & " Fax <br />"&vbCrLf & "<a
    href=""" &strWeb & """ style=""FONT-SIZE: 12pt; COLOR:#000080; FONT-FAMILY: Garamond""" & ">" & strWebD & "<" & Chr(47) & "a>"&vbCrLf & "<br />"&vbCrLf
       end if

    ' Open Original file
       Err.Clear
       Set objFile = objFSO.OpenTextFile(strHTMFileOrig, 1)
       strFileTextOld = objFile.ReadAll 

       objFile.Close

    '  Replace signature block with new block
       strFileTextNew = Replace (strFileTextOld, strSigBlockOld, strSigBlockNew)

    ' Create new file with new block
       Set objFile = objFSO.CreateTextFile(strHTMFileDest, boolUpdateStyle, False) 
       objFile.WriteLine strFileTextNew
       objFile.Close

    '==========================================================================
    ' Modify TXT File and create at destination
    '========================================================================== 

       If strCell <> "" then
    '         strSigBlockNew = strFullName & vbCrLf & strTitle & vbCrLf & strCompany & vbCrLf & vbCrLf & strCity & vbCrLf & strTel & vbCrLf & strCell & " Cell" & vbCrLf & strFax & " Fax" & vbCrLf & strWeb & vbCrLf 

       else 
             strSigBlockNew = strFullName & vbCrLf & strTitle & vbCrLf & strCompany & vbCrLf & vbCrLf & strCity & vbCrLf & strTel & vbCrLf & strFax & " Fax" & vbCrLf & strWeb & vbCrLf 

       end if

    ' Open Original file
       Err.Clear
       Set objFile = objFSO.OpenTextFile(strTXTFileOrig, 1)
       strFileTextOld = objFile.ReadAll 
       objFile.Close
    ' Replace signature block with new block
       strFileTextNew = Replace (strFileTextOld, strSigBlockOld, strSigBlockNew)
    ' Create new file with new block
       Set objFile = objFSO.CreateTextFile(strTXTFileDest, boolUpdateStyle, False)
       objFile.WriteLine strFileTextNew
       objFile.Close

    '==========================================================================
    ' Tidy-up
    '==========================================================================

       set objFile = Nothing
       set objFSO  = Nothing

                                             
    • Moved by Bill_Stewart Wednesday, January 20, 2016 7:52 PM This is not "fix script I found on the Internet for me" forum
    Friday, December 18, 2015 9:54 PM

All replies

  • Please look in Gallery as there are many versions that create signatures from AD information.  We do not use text files for this. Use the Outlook/Word signature builder to do this and it will be very easy.

    If you are on Exchange 2010 or later then Exchange can add a signature with AD info to all messages.


    \_(ツ)_/

    Friday, December 18, 2015 10:22 PM
  • Another suggestion. Never start a scrtip with "On Error Resume Next ".  YOU must handle every possible error if you do this and you are ignoring all errors.

    \_(ツ)_/

    Friday, December 18, 2015 10:23 PM
  • I'll take a look at the gallery but just so you'll know, "signature" that you add to all messages via Exchange puts the signature at the very bottom of the email - regardless of how many replies/forwards are in an email chain. I did explore that first.
    Friday, December 18, 2015 10:34 PM
  • Only put the signature on new outgoing messages.

    The Outlook signature builder does everything you are doing  in a few lines.  Don't manually build the files.  They are best when auto-built by Word.


    \_(ツ)_/

    Friday, December 18, 2015 10:41 PM
  • That is not an option.  We have legal disclaimers that must accompany the email whether it is a reply, a forward, or a new email.  Ideally, that disclaimer would be directly below the response and not at the very bottom of the email chain. 

    I am not familiar with the signature builder that you're talking about. 

    I did figure out the txt file portion if anyone is interested but I do still want to know about the signature builder that you discuss.

    The fix: I opened as Unicode and it worked fine.

       Set objFile = objFSO.OpenTextFile(strTXTFileOrig, 1,0,-1)

    Instead of

       Set objFile = objFSO.OpenTextFile(strTXTFileOrig, 1)  

    Friday, December 18, 2015 10:53 PM
  • Here is one example: https://gallery.technet.microsoft.com/6f7eee4b-1f42-499e-ae59-1aceb26100de

    Her are many exmples: https://gallery.technet.microsoft.com/site/search?query=signature&f%5B2%5D.Value=signature&f%5B2%5D.Type=SearchText&f%5B0%5D.Value=Office&f%5B0%5D.Type=RootCategory&f%5B0%5D.Text=Office&f%5B1%5D.Value=Microsoft%20Outlook&f%5B1%5D.Type=SubCategory&f%5B1%5D.Text=Microsoft%20Outlook&ac=4

    Once you understand how the signature builder works you will find that it can be done very easily.

    Create a Word signature template with fields which you fill in before calling the signature generator.  This allows you to use Word to design as fancy a signature as you need and you can change it without having to change the script.


    \_(ツ)_/

    Friday, December 18, 2015 11:03 PM