Option Explicit
dim tts, fs, wsh, ts, strFile, strText, strOld, intLine, intSpace
Const ForReading = 1
	'Check argument count
	If WScript.Arguments.Count <> 1 Then
		MsgBox "Drop a text file on this script and I'll read it to you."
		WScript.Quit 1
	End If
	'Make sure the argument is a file
	Set fs = CreateObject("Scripting.FileSystemObject")
	strFile = WScript.Arguments(0)
	strFile = Trim(strFile)
	If strFile = "" Then
		MsgBox "I can't read nothing! Give me the name of a file."
		WScript.Quit 1
	End If
	If InStr(strFile, "\\") <> 1 And InStr(strFile, ":\") <> 2 Then
		strFile = fs.GetAbsolutePathName(fs.BuildPath(fs.GetFolder(".").Path, strFile))
	End If
	If Not fs.FileExists(strFile) Then
		MsgBox "I can't locate a file named """ & WScript.Arguments(0) & """. Try using a fully-qualified path and file name."
		WScript.Quit 1
	End If
	'Register a text-to-voice engine. Any engine!
	TTSEngine
	If tts Is Nothing Then
		InputBox "You don't have a voice! Please hop on the internet, download, and install the Microsoft speech engine from here:", "Text Reader", "http://activex.microsoft.com/activex/controls/sapi/spchapi.exe"
		WScript.Quit 1
	End If
	'Force CSCRIPT
	If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
		If InputBox ("You are running this script with WSCRIPT. If you press ""OK"", reading will take place invisibly. " & vbCrLf & "To read visibly THIS TIME ONLY, press ""Ctrl-C"" to copy all the below text. Then press your ""Start"" button, select ""Run"", and press ""Ctrl-V"" to paste it into the ""Open"" dialog. Then click OK. Then you can ""Cancel"" this box." & vbCrLf & "To read visibly NEXT TIME (and every time thereafter), execute this command (in the Start/Run dialog or in a DOS box):" & vbCrLf & "CSCRIPT.EXE //H:CScript //NoLogo //S", "Text Reader" , "cscript.exe """ & WScript.ScriptFullName & """ """ & WScript.Arguments(0) & """") = "" Then
			WScript.Quit 1
		End If
	End If
	'Read the file one line at a time
	Set ts = fs.OpenTextFile(WScript.Arguments(0), ForReading, True)
	strOld = ""
	Do Until ts.AtEndOfStream
		strText = strOld
		Do Until InStr(strText, ".") <> 0 
			strText = strText & " " & ts.ReadLine
			If ts.AtEndOfStream Then Exit Do
		Loop
		strOld = Mid(strText, InStr(strText, ".") + 1)
		strText = Left(strText, InStr(strText, "."))
		For intLine = 75 To Len(strText) Step 75
			intSpace = InStrRev(strText, " ", intLine)
			strText = Left(strText, intSpace) & vbCrLf & Mid(strText, intSpace + 1)
		Next 
		Status vbCrLf & strText
		Say strText
	Loop
	Set tts = Nothing

Sub Say(strText)
Dim blnIsSpeaking
	On Error Resume Next 'Ignore unsupported methods and arguments!
	Err.Clear
	tts.Speak strText
	If Err.Number <> 0 Then tts.Speak strText, 16
	'The value 16 is "READING" in SAPI 4, and "NOT_XML" in SAPI 5
	blnIsSpeaking = False
	blnIsSpeaking = tts.IsSpeaking 'Unsupported method will not change value of blnIsSpeaking
	Do While blnIsSpeaking
		WScript.Sleep 500
		blnIsSpeaking = tts.IsSpeaking
	Loop
End Sub

Sub Status(strMessage)
	If Lcase(Right(Wscript.FullName, 12)) = "\cscript.exe" Then
		Wscript.Echo strMessage
	End If
End Sub

Sub TTSEngine()
	On Error Resume Next
	Set tts = Nothing
	
	'C:\Program Files\Common Files\Microsoft Shared\Speech\sapi.dll
	'Microsoft Speech Object Library
	'SpeechLib.SpVoice
	'TypeName "SpVoice"
	Set tts = CreateObject("Sapi.SpVoice")
	If Not(tts Is Nothing) Then Exit Sub

	'C:\WINNT\speech\vcmd.exe
	'C:\WINNT\Speech\vtxtAuto.tlb
	'Voice Text Object Library
	'Voice Text 1.0 Type Library
	'VTxtAuto.VTxtAuto
	'TypeName "IVTxtAuto"

	Set tts = CreateObject("Speech.VoiceText")
	tts.Register "", " "
	tts.Enabled = True
	If Not(tts Is Nothing) Then Exit Sub
	
	Set tts = CreateObject("Speech.VoiceText.1")
	tts.Register "", " "
	tts.Enabled = True
	If Not(tts Is Nothing) Then Exit Sub
	
	'C:\WINNT\Speech\vtext.dll
	'Microsoft Voice Text
	'HTTSLib.TextToSpeech
	'TypeName "TextToSpeech"

	Set tts = CreateObject("TextToSpeech.TextToSpeech")
	tts.Enabled = True
	If Not(tts Is Nothing) Then Exit Sub
	
	Set tts = CreateObject("TextToSpeech.TextToSpeech.1")
	tts.Enabled = True
	If Not(tts Is Nothing) Then Exit Sub

	'C:\WINNT\Speech\Xvoice.dll
	'Microsoft Direct Speech Synthesis
	'ACTIVEVOICEPROJECTLib.DirectSS
	'TypeName "DirectSS"

	Set tts = CreateObject("ActiveVoice.ActiveVoice")
	tts.Enabled = True
	If Not(tts Is Nothing) Then Exit Sub

	Set tts = CreateObject("ActiveVoice.ActiveVoice.1")
	tts.Enabled = True
	If Not(tts Is Nothing) Then Exit Sub

	On Error Goto 0
End Sub

