Windows has a cool feature that lets you make your computer say or say whatever you say to it! This method takes advantage of a built-in API found in Windows XP, Vista, 7, 8 and 10 called SAPI (Speech Programming Interface).
The Microsoft Speech API is what is used for the text-to-speech accessibility feature built into Windows. You can also write programs that call APIs, allowing your applications to speak, but that’s for developers.
It’s actually very easy to get your computer to say what you want! In this article, I’ll show you how to create a VBS script that accepts user input and then speaks it out. I’ll also show you how to hard-code the text you want to read, and then schedule that script to run on certain events, such as Windows startup or logoff.
Input text, pronounce the message
Let’s start with a simple script that will open a dialog box where you can enter what you want Windows to say. Open Notepad first.
Step 1. Paste the following text into a new document:
Faint message, sapi
message = InputBox (“Enter the text you want to speak,†“Speakâ€)
Install sapi = CreateObject (“sapi.spvoice”)
sapi.Speak message
Note that when you copy text from a web browser and paste it into Notepad, the quotes will cause problems. Before saving the file, you need to review and delete each quote (“) and re-enter the quote. The example below has six quotes. It should look something like the image below.
Now, when you are going to save the file, give it any name, but make sure you also type .VBS after the name. Then, in the Save as type box, select All files instead of default text documents.
Now double click on the VBS file and you should see an input box where you can enter the text you want to speak! Enter something and click OK.
If you did everything correctly, you should hear a male or female voice pronouncing your phrase. If you receive any error message, be sure to copy and paste the text again and replace those quotes.
Configure SAPI Voice settings
It’s fun, but we can also customize our little talking computer with different settings. We can change the volume, speed or slowness of the voice, and also change the gender from male to female. Here’s some sample code where I’ve added a couple of extra lines.
Faint message, sapi
message = InputBox (“Enter the text you want to speakâ€, “Speakâ€)
Install sapi = CreateObject (“sapi.spvoice”)
Install sapi.Voice = sapi.GetVoices.Item (1)
sapi.Rate = 0 em>
sapi.volume = 100
sapi.Speak message
The default speed is 0, the range is -10 to 10. -10 means the voice will speak very slowly and 10 means very fast. The default volume is 100 and the range is 0 to 100. The line that starts with Set sapi.Voice will allow you to switch to a different voice if installed on your system.
Please note that the number of voices you can choose depends on the operating system. There is only one voice in Windows 7, Microsoft Anna.
Windows 10 has two voices, Microsoft David and Microsoft Zira, as shown below.
If you only have one voice installed on your system, you need to change what sapi.GetVoices.Item (1) says to sapi.GetVoices.Item (0) em> < / em>, otherwise you will get an error when you try to run the script. You can also create another Notepad document and paste the code below into it that will tell you which voices are installed. Save it as a .VBS file as shown above and run.
Install VObj = CreateObject (“SAPI.SpVoice”)
For each voice in VObj.getvoices
I = I + 1 em>
msgbox “” & (I – 1) & “-” & Voice.GetDescription
Next
So far, we have used this popup dialog to enter text, but you can also just type your message into the script file. This would be useful if you want to schedule the script to run automatically. Obviously this can be used to prank your friends and family and it works really well.
Imagine that someone turns on their computer and hears the computer say to him, “Hi John, I really want to sleep, please don’t bother me today!” when it boots up! From experience I can promise that this is hysterical and you should definitely try it.
To hardcode the message, just change the last line to something like this:
sapi. Say, “Hi John, I’m really tired today!”
Schedule a script file
Now that you’ve hard-coded your message for the intended purpose, you just need to fire it whenever you want. If you’re tech-savvy, you can use a program like AutoIt, which will allow you to do extreme things, like running a script file when a certain program is open or when windows are maximized or minimized.
I don’t have time to go into detail on this in this post, but luckily Microsoft has a built-in feature called Task Scheduler that makes it easy to do fairly complex scripted tasks.
My previous post went into detail about how to schedule a script file to run when certain events occur on Windows. If you have any problems getting the script to work, feel free to leave comments and I’ll try to help. Enjoy!
–