none
VBS script to speak the appropriate greeting ie good morning, good afternoon, good evening RRS feed

  • Question

  • New to the board! Hope everyone is having a great new year.

    I am here to seek a little help with a vbs script.

    I worked extensively with VB but that back in the vb5 era.

    So I need help with the following code.

    It is from Excel but I would like to do the same with vbs

    In excel I can do this:

    'Dim PresentTime
    'PresentTime = Format(Now, "hh")
    'With Application.Speech
    'Select Case PresentTime
    'Case Is < 11: .Speak "Good morning, how are you this morning?"
    'Case 12 To 16: .Speak "Good afternoon,  how are you this afternoon?"
    'Case Is > 16: .Speak "Good evening,  how are you this evening?"
    'End Select
    'End With

    Is there a way to do the same with VBS?

    Trying to incorporate it into this vbs script.

    Dim speaks, speech
    WScript.Echo Hour(dtmValue)
    speaks="Welcome to your PC"
    Set speech=CreateObject("sapi.spvoice")
    speech.Speak speaks

    I really do appreciate your help AND patience with a newbee!



    • Edited by dribkcalb Saturday, January 28, 2017 7:34 AM
    Saturday, January 28, 2017 7:28 AM

Answers

  • VBScript equivalent.

    Set sapi = CreateObject("SAPI.SpVoice")
    
    Select Case Hour(Now)
    	Case 11
    		msg = "Good morning, how are you this morning?"
    	Case 12,13,14,15
    		msg = "Good afternoon,  how are you this afternoon?"
    	Case 16
    		msg = "Good evening,  how are you this evening?"
    	Case Else
    		msg = ""
    End Select
    If msg <> "" Then sapi.Speak( msg )
    

    Of course your original VBA code won't work and the VBScript above will not work in the same way.  The only way it will work is if you schedule it to run once an hour.


    \_(ツ)_/



    • Edited by jrv Saturday, January 28, 2017 9:05 AM
    • Marked as answer by dribkcalb Saturday, January 28, 2017 9:12 PM
    Saturday, January 28, 2017 9:02 AM

All replies

  • In PowerShell:

    $sapi = New-Object -ComObject sapi.spvoice
    $sapi.Speak('Don't use VBScript.  Use PowerShell')

    That is all you need to do to use SAPI.  We don't use VBS anymore.  If you are going to learn anything learn PowerShell.


    \_(ツ)_/

    Saturday, January 28, 2017 8:27 AM
  • $sapi = New-Object -ComObject sapi.spvoice
    while(1){
    	$msg = switch -regex ([datetime]::Now.ToString('hh-mm')){
    		'10-00' 	{'Good morning, how are you this morning?'}
    	 	'1[2-6]-00' {'Good afternoon,  how are you this afternoon?'}
    		'16-00' 	{'Good evening,  how are you this evening'}
    	}
    	if($msg){$sapi.Speak($msg)}
    	sleep 60
    }
    


    \_(ツ)_/

    Saturday, January 28, 2017 8:42 AM
  • VBScript equivalent.

    Set sapi = CreateObject("SAPI.SpVoice")
    
    Select Case Hour(Now)
    	Case 11
    		msg = "Good morning, how are you this morning?"
    	Case 12,13,14,15
    		msg = "Good afternoon,  how are you this afternoon?"
    	Case 16
    		msg = "Good evening,  how are you this evening?"
    	Case Else
    		msg = ""
    End Select
    If msg <> "" Then sapi.Speak( msg )
    

    Of course your original VBA code won't work and the VBScript above will not work in the same way.  The only way it will work is if you schedule it to run once an hour.


    \_(ツ)_/



    • Edited by jrv Saturday, January 28, 2017 9:05 AM
    • Marked as answer by dribkcalb Saturday, January 28, 2017 9:12 PM
    Saturday, January 28, 2017 9:02 AM