Minor correction to the script.  It wasn't spacing properly around commas...

'Option explicit

'msgbox CapitalizeValue(WScript.Arguments(0))
'wscript.quit()

Function CapitalizeValue(sStringIN)
   Dim i
   Dim s
   Dim sNewName
   Dim sChar
   Dim arString
   Dim bIsCapital
   Dim bCapitalizeNext

   'Get rid of:
   '  leading or trailing blanks,
   '  Double Spaces, space comma space
   '  Existing Caps
   sStringIN = Replace(sStringIN,","," , ")
   sStringIN = Trim(LCase(DropDblSpace(sStringIN)))
   ' replace comma with space comma space to preserve comma
   if sStringIN = "" then
     CapitalizeValue = sStringIN
   end if
   bCapitalizeNext = true
   For i=1 to Len(sStringIN)
         sChar = Mid(sStringIN,i,1)
         if (bCapitalizeNext = true) then
             if IsLetter(sChar) = true then
                sChar = UCase(sChar)
                bCapitalizeNext = False
             else
                bCapitalizeNext = True
             end if
         else
           if IsLetter(sChar) = false then
               bCapitalizeNext = true
            end if
         end if
        sNewName = sNewName & sChar
   Next
    ' Check for Roman Numerals as Last element of sNewName
    arString = Split(sNewName," ")
    if ubound(arString) > 0 then
      for i = 0 to ubound(arString)
          arString(i) = Trim(arString(i))
          if isRoman(arString(i)) = True then
            arString(i) = ucase(arString(i))
      end if
      next
      for i = 0 to ubound(arString)
        If (arString(i) <> "") or (arString(i) <> " ") then
        'msgbox cstr(i) & " #" & arstring(i) & "#"
          if arString(i) <> "," then
            CapitalizeValue =  CapitalizeValue & " " & arString(i)
          else
            CapitalizeValue =  CapitalizeValue & arString(i)
          end if
        End if

      Next
    else
      CapitalizeValue = sNewName
     end if
     CapitalizeValue = Trim(Replace(CapitalizeValue,",",", "))
end Function

function DropDblSpace(sStr)
Dim s
Dim r
s = Chr(20) & Chr(44)
r = Chr(44)
 while InStr(sStr,s) > 0
   sStr = Replace(sStr,s,r)
  wend
 While Instr(1,sStr,"  ") > 0
   sStr = Replace(sStr, "  ", " ")
 wend
   DropDblSpace = sStr
end function

function IsLetter(sChar)
  Dim i
  i = CInt((Asc(sChar)))
  IsLetter = False
  if ((i >=65) and (i <=90)) Then
    IsLetter = true   ' Upper case letters
  End if
  if ((i >=97) and (i <=122)) Then
    IsLetter = True ' Lower case letters
  End if
end function

Private Function IsRoman(ByVal sStr)
  Dim i
  Dim s
  IsRoman = TRUE
  s = Trim(UCase(sStr))
  for i = 1 to len(s)
    if inStr(1, "MDCLXVI", mid(s,i,1)) = 0 then
      isRoman = False
      exit For
    end if
   Next
End Function

Reply via email to