Narrator Pitch on Variables in VBS - vbscript

I am writing a script in VBS for when I log in I am greeted with the time. However, the XML variable for voice pitch is not functioning without text that is static. Am I missing something?
Sapi.speak "<PITCH MIDDLE = '5'/>Good Morning"
This runs at a higher pitch...
Sapi.speak minute(time)
While this is normal.
I have tried "<PITCH MIDDLE = '5'/> minute(time)", <PITCH MIDDLE = '5'/>middle(time), and "<PITCH MIDDLE = '5'/>"middle(time).
All of which return with errors.
I appreciate any help that is given. :)

What you pass to the Speak method is a string. You are trying to pass a string and a number, so, all you should need to do is to concatenate both using the & concatenate operator to obtain a string with the full content
Sapi.speak "<PITCH MIDDLE = '5'/>" & Minute(Time)

Related

vbscript: RegExp Replace turn capture group into variable

regex in VBscript has 3 methods, Test, Extract and Replace, but I can only seem to turn capture groups from Extract into variable.
However what I want to do is use capturing groups from 'Replace' as a variable. I can get a Regex.Replace working with no problems using $1 $2 etc for capturing groups, however I want multiply one of the capture groups.
In an xml file, I want to extract a value, times it by 15, and insert it back in. In this example the tag.
e.q.
strText = "
<rte>
<name>gpx.studio church 2 reduced.gpx</name>
<rtept lat='-33.482652' lon='150.159134'>
<ele>938.4</ele>
<desc>076</desc>
</rtept>
<rtept lat='-33.4825698175265' lon='150.159515440464'>
<ele>942.3</ele>
<desc>162</desc>
</rtept>
<rtept lat='-33.4828785376496' lon='150.159633457661'>
<ele>943.4</ele>
<desc>098</desc>
</rtept>
</rte>
</gpx>"
Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Global=True
oRegExp.Multiline = True
oRegExp.Pattern = strPattern
strPattern = "(<rtept(?:(?:.|\n|\r)*?))<desc>(.*?)<\/desc>((?:(?:.|\n|\r)*?)<\/rtept>)"
strReplace = "$1<desc>$2<\/desc>$3"
' so on this line above, I want to turn the $2 into an integer and multiply it by 15 before putting back into replace.
' I have not done it here because I know it doesnt work as "$2"x1000
strNewText = oRegExp.Replace(strText, strReplace)
I want to turn the $2 into an integer and multiply it by 15 before putting back into replace.
I have tried to get the capture groups as SubMatches(1) which work with Regex.Extract method but it doesnt seem to work in Regex.Replace method, unless I am missing something....
help appreciated

Accept a name in Text Box, Print it when someone click on Print Button

I have a TextBox in VB and a Command Button. I want to print the string upon clicking on command button.
I am using the following code, please tell what I am doing wrong:-
Dim name As String
name = Val(Text1.Text)
MsgBox ("Welcome" & Str(name))
When I input a string in Textbox and click on command button, result is:
Welcome 0
Leave out the val() around your Text1.Text, val() returns the numbers up to the first symbol it can't recognize as a number used in a String. See the documentation. I guess you used a 0 in your string in the TextField or no number at all, both would return 0.
Additionally, it is unnecessary to cast your String name to a String since it is already a String so you can also leave out the Str().
The val function returns the numeric representation of its argument, otherwise it returns "0". It's a bit hard these days to find the official VB6 documentation, but you may want to check: https://en.wikibooks.org/wiki/Visual_Basic/VB6_Command_Reference#Val
So, in your example, if you enter any number in the Text1 textbox control, you will see it in your message box. If you enter any text, you will get "Welcome 0", as you do now. Therefore, you have to remove the val function from your code, like:
Dim name As String
name = Text1.Text
MsgBox ("Welcome " & name)
maybe even simplifying it to:
MsgBox("Welcome " & Text1.Text)
So you declared a string varaible namewhich you want to fill with the text from the Text1box. So you need to spare the val(...)part.
Second, as namealready represents a string, leave out the strin the message box:
name = Text1.Text
MsgBox ("Welcome " & name)

VBS convert string to floating point

Dim strnumber
strnumber = "0.3"
Dim add
add = 0.1
Dim result
result = strnumber + add
MsgBox result
I want to get 0.4 as result, but get 3.1.
I tried clng(strnumber) and int(strnumber), nothing works. There is a simple solution for sure but I won't find it.
EDIT: Solution
result = CDbl(Replace(strnumber,".",",") + add
Has to do with your locale settings. Automatic conversion (as well as explicit one) observes it in the same manner as in CStr() function.
E.g. in my locale CStr( 0.3) results to 0,3 that is invert to CDbl("0,3") while CDbl("0.3") results to an error.
BTW: always use option explicit and, for debugging purposes, On Error Goto 0
Following below procedures can help:
Replacing the dot(".") with comma (",") in the string
change the string to double by Cdbl
example:
dim a,b,c
a="10.12"
b="5.05"
a=Replace(a,".",",")
b= Replace(b,".",",")
c=Cdbl(a)+Cdbl(b)
msgbox c
You want to add two numbers. So you should use numbers (and not a string (strnumber) and a number (add):
>> n1 = 0.3
>> n2 = 0.1
>> r = n1 + n2
>> WScript.Echo r
>>
0,4
As you can see from the output (0,4), I'm using a locale (German) that uses "," as decimal 'point'. But literals always use ".". So by using the proper data types you can write your scripts in a language/locale independent fashion as long as you don't need to process external string data (from a file or user input). Then you have to modify the input before you feed it to a conversion function like CDbl(). For simple cases that can be done with Replace(inp, badmarker, goodmarker).
P.S. You said you " tried clng(strnumber) and int(strnumber)". You should have tried CDbl(). CLng() tries to get a long integer (cf. CInt()), Int() removes/rounds the fraction from number.

Extracting strings and writing to a new file

I was googling around but didn't find the right answer, perhaps people from here are willingly and able to help me.
I'm very new to VBS or WSH and I like to have a solution for this problem:
I'm searching for textstrings within a file without a line break (only one line). The textstrings I'm looking for start always with the same content "jpgline" and ends with the three letters "qbm". How can we extract each sentence (the strings are always 64 chars long) containg "jpgline....qbm" into a separate file.
I'm looking for a solution in Visual Basic Script as I use Windows 7.
Thanks in advance
M i k e
Use a regular expression:
Set re = New RegExp
re.Pattern = "^jpgline.*qbm$"
re.IgnoreCase = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile("C:\path\to\input.txt")
Set outFile = fso.OpenTextFile("C:\path\to\output.txt", 2, True)
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
If re.Test(line) Then outFile.WriteLine line
Loop
inFile.Close
outFile.Close
As your input file has no lines, use .ReadAll() to load its entire content into a string variable. Apply a RegExp to get all parts (Matches) defined by the pattern "jpgline.{N}qbm" where N is either 64 or 64 - the length of the pre/suffix. Ansgar has shown how to open and write to the output file.
Use the RegExp Docs to learn about .Execute and how to loop over the resulting match collection. The docs will tell you about .Test too.

Need to find character within apostrophe substring and replace it, any ideas?

Ok, let me preface this question with the fact that I am extremely new to vb script, but I need to make an addition to a script that another developer created and he is away.
Problem. :
I am getting data that is feed into a process. The script I already have handles some things that I won't go into. One thing I found out that it doesn't handle correctly is commas that occur in double quotes.
So I get some data like this. :
Joe, Bill, James, Alex, "Google, Inc", Rose, "Kickstarter, LLC"
What happens is that when this script kicks to the process application it blows up because of these commas in the double quotes. I need to be able to have this script go in and replace those commas in the Google and Kickstarter blocks with something a little more unique.
If you have the answer to this I would appreciate it, but I wouldn't mind some direction either of something that does something like this, etc.
Any answers are greatly appreciated. Again very new to vbascript just started reading syntax on it today.
You left out a few details about the format of the input data and the replacement text. However, the approach you need to take is to use the regular expression capability of VBScript to identify patterns in your input data, then use the "Replace" method to replace them. Here's a short MS article about regular expressions in VBScript.
Here's an example of what it might look like. This example is definitely NOT bullet proof and it makes some assumptions, but I think it will get you started:
Dim re, strstr, newstrstr
Dim inputstrarray(7)
Set re = new regexp
inputstrarray(0) = "Joe"
inputstrarray(1) = "Bill"
inputstrarray(2) = "James"
inputstrarray(3) = "Alex"
inputstrarray(4) = """" & "Google, Inc" & """"
inputstrarray(5) = "Rose"
inputstrarray(6) = """" & "Kickstarter, LLC" & """"
re.pattern = ",\s" 'pattern is a comma followed by a space
For Each strstr In inputstrarray 'loop through each string in the array
newstrstr = re.Replace(strstr, "_") 'replace the pattern with an underscore
If StrComp(strstr,newstrstr) Then
Wscript.Echo "Replace " & strstr & " with " & newstrstr
Else
Wscript.Echo "No Change"
End If
Next
Output looks like the following:
No Change
No Change
No Change
No Change
Replace "Google, Inc" with "Google_Inc"
No Change
Replace "Kickstarter, LLC" with "Kickstarter_LLC"
No Change

Resources