VBS End of the statement expected error in - vbscript

strCode = "Private Sub AcclvsTime() " & vbCr _
& "Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225) "& vbCr _
& "myChtObj.Chart.ChartType = 4 " & vbCr _
& "myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")" & vbCr _
& "End Sub"
objWriteExcel.VBE.ActiveVBProject.VBComponents.Item("Sheet1").CodeModule.AddFromString(strCode)
When I executed this code i got the error “end of the statement expected in line 4” (& "myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")" & vbCr _)
Can any one help me where is the mistake?

#paxdiablo: I would comment, but don't see a comment button.
Notice, though, the second quote from Range("A1:B15")" & vbCr is started from "myChtObj.Chart.SetSourceData
Having the full block of code would help better, as we can't tell what kind of end statement you will need. I.e, your "End Sub" is in double quotes. If that's the end of the sub, you need to take them out.

Building strings by concatenation is cumbersome and errorprone. Especially, if the result is a multiline string, use Join:
strCode = Join( Array( _
"Private Sub AcclvsTime()" _
, " Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225)" _
, " myChtObj.Chart.ChartType = 4" _
, " myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets(""sheet2"").Range(""A1:B15"")" _
, "End Sub" _
), vbCrLf)
WScript.Echo strCode
output:
Private Sub AcclvsTime()
Set myChtObj = oExcelWriteWorkSheet.ChartObjects.Add(100,375,75,225)
myChtObj.Chart.ChartType = 4
myChtObj.Chart.SetSourceData objWriteWorkbook.Sheets("sheet2").Range("A1:B15")
End Sub
to reduce the noise caused by & and the repeating stuff vbCr(Lf). That will improve your chances to see the problems/mistakes. (Exactly two literals - "sheet2", "A1:B15" - to quote).
Addional Remark:
Given that the culprit is:
"whatever("sheet2").Range("A1:B15")"
it is obvious, that remedy
"whatever(""sheet2"").Range(""A1:B15"")"
is easier to read/check/write and less errorprone than
"whatever(" & Chr(24) & "sheet2" & Crh(34) & ").Range(" & Chr(34) + "A1:B15" & Chr(32) & ")"
Avoiding "" in literals by splicing in & Chr(34) &s is a bad strategy.

" ... Range("A1:B15")" & vbCr
Note those quotes within quotes on your fourth line (for both "sheet2" and "A1:B15") - you need to fix that.
If you want to put quotes within quotes, you can do it thus, by escaping. Two consecutive " characters within a double-quoted string will be translated to a single ".
"the word ""xyzzy"" is quoted"
Alternatively, you can also use chr(34) to get the quote:
"the word " & chr(34) & "xyzzy" & chr(34) & " is quoted"
This may be preferable in more complex cases, though I've rarely had a need for it.

Related

right syntax for create subscription in stripe from classic.asp [duplicate]

I have this code:
a = "xyz"
g = "abcd " & a
After running it, the value of g is abcd xyz.
However, I want quotes around the value of a in g. After running the code, g should be abcd "xyz" instead.
How can I accomplish this?
You can escape by doubling the quotes
g="abcd """ & a & """"
or write an explicit chr() call
g="abcd " & chr(34) & a & chr(34)
You have to use double double quotes to escape the double quotes (lol):
g = "abcd """ & a & """"
I usually do this:
Const Q = """"
Dim a, g
a = "xyz"
g = "abcd " & Q & a & Q
If you need to wrap strings in quotes more often in your code and find the above approach noisy or unreadable, you can also wrap it in a function:
a = "xyz"
g = "abcd " & Q(a)
Function Q(s)
Q = """" & s & """"
End Function
The traditional way to specify quotes is to use Chr(34). This is error resistant and is not an abomination.
Chr(34) & "string" & Chr(34)
You can do like:
a="""xyz"""
g="abcd " & a
Or:
a=chr(34) & "xyz" & chr(34)
g="abcd " & a
I don't think I can improve on these answers as I've used them all, but my preference is declaring a constant and using that as it can be a real pain if you have a long string and try to accommodate with the correct number of quotes and make a mistake. ;)
I designed a simple approach using single quotes when forming the strings and then calling a function that replaces single quotes with double quotes.
Of course this approach works as long as you don't need to include actual single quotes inside your string.
Function Q(s)
Q = Replace(s,"'","""")
End Function
...
user="myself"
code ="70234"
level ="C"
r="{'User':'" & user & "','Code':'" & code & "','Level':'" & level & "'}"
r = Q(r)
response.write r
...
Hope this helps.
I found the answer to use double and triple quotation marks unsatisfactory. I used a nested DO...LOOP to write an ASP segment of code. There are repeated quotation marks within the string. When I ran the code:
thestring = "<asp:RectangleHotSpot Bottom=""" & bottom & """ HotSpotMode=""PostBack"" Left="""& left & """ PostBackValue=""" &xx & "." & yy & """ Right=""" & right & """ Top=""" & top & """/>"
the output was:
<`asp:RectangleHotSpot Bottom="28
'Changing the code to the explicit chr() call worked:
thestring = "<asp:RectangleHotSpot Bottom=""" & bottom & chr(34) & " HotSpotMode=""PostBack"" Left="""& left & chr(34) & " PostBackValue=""" &xx & "." & yy & chr(34) & " Right=""" & right & chr(34) & " Top=""" & top & chr(34) &"/>"
The output:
<asp:RectangleHotSpot Bottom="28" HotSpotMode="PostBack" Left="0" PostBackValue="0.0" Right="29" Top="0"/>

Does VB6 InputBox support a multi-line string?

When using InputBox in Visual Basic 6 can I input a multiline string?
strSrch = InputBox("Enter word(s) or phrase(s), like " & Chr(34) & "Jesus wept" & _
Chr(34) & " to search for any word or phrase" & vbCrLf & _
"If you place & between words the verse must contain both words, loved & world" & _
vbCrLf & "or both phrases, " & Chr(34) & "keep the commandments" & Chr(34) & _
" & " & Chr(34) & "of Jesus" & Chr(34) & vbCrLf & "Put ! in front of a word or phrase to exclude it from your search, Jesus ! testimony" & _
vbCrLf & "Use( before and ) after expressions to group them together, (" & Chr(34) & _
"Come unto me" & Chr(34) & " & all)" & vbCrLf & "Use Xor between two words to include one or the other but not both, " & _
Chr(34) & "I am" & Chr(34) & " Xor that", "Word Search")
No, not really. IIRC, the InputBox should preserve any newline chars that are dropped into it via a paste or entered via ALT+###. But, they will appear as spaces in the box.
It will only ever appear on a single line. There's no real way to use an InputBox to let a user enter multiple lines.
The most feature-capable way to handle this would be to create a custom form and use it to collect user input. You can make it a modal dialog to force the user to interact with it, similar to an InputBox. There are a few ways to pass the input back to the procedure that displayed the form (such as using a global variable).

how to assign XML values to string in Vb6

I want to assign below mentioned xml values to a string like this
Dim test As String
test = ... ?
Where the XML should contain:
<RptVer>1</RptVer>
<RptTyp>1</RptTyp>
</RptInfo>
</InstRptRoot>
How can I do this and also preserve the formatting (ie linebreaks, spacing, etc.)?
Mark answered your question, I'll answer your second question:
Dim test As String
test = "<RptInfo>" & vbCrLf & vbCrLf & _
vbTab & "<RptVer>1</RptVer>" & vbCrLf & vbCrLf & _
vbTab & "<RptTyp>1</RptTyp> & vbCrLf & vbCrLf & _
"</RptInfo>"
Assuming you want it double-spaced and indented. You had also missed the leading tag, but MarkL caught that as well.

How to set Double Quotes using VBScript Dictionary in UFT 12.0

Here I want to Add double quotes (") in my sentence, but it insert Two (") at single place as shown in picture. Have tried Chr(34) and Replace().
Please help me to insert double quotes in Dictionary Item using VBScript in UFT12.
I don't want to use Msgbox to display this String.
Dim Dict
Set Dict=CreatObject("Scripting.Dictionary")
Dic("Value")="I went to mall....Where I met my Friends "&Chr(32)&"XYZ" & "DEF"&Chr(34)&". We enjoied a lot."
When I "Watched" Dic("Value") using UFT12.0 It gives shows :
"I went to mall....Where I met my Friends ""XYZ & DEF"". We enjoied a
lot."
Your debugger tool displays the value of a string variable as a literal (what you would have to type/paste into your editor). Evidence: the surrounding double quotes.
Your .. "&Chr(32)&"XYZ" & "DEF"&Chr(34)&". .. proves: Using Chr() is inferior (extra function call and noise) and error prone (Chr(wrong number) to escaping " by "".
Here's an example, in addition to exhaustive Ekkehard.Horner's answer (returns the string and the position of the first occurrence of " and doubled "" in that string):
option explicit
Dim myString0, myString1, myString2, myString4
myString0 = "none double quotes"
myString1 = "with ""double"" quotes 1"
myString2 = "with " & Chr(34) & "double" & Chr(34) & " quotes 2"
myString4 = "twice """"double"""" quotes"
Wscript.Echo Wscript.ScriptName & ": Using Double Quotes" _
& vbNewLine & myString0 _
& vbTab & InStr( 1, myString0, Chr(34) , vbTextCompare) _
& vbTab & InStr( 1, myString0, Chr(34)&Chr(34), vbTextCompare) _
& vbNewLine & myString1 _
& vbTab & InStr( 1, myString1, Chr(34) , vbTextCompare) _
& vbTab & InStr( 1, myString2, Chr(34)&Chr(34), vbTextCompare) _
& vbNewLine & myString2 _
& vbTab & InStr( 1, myString2, Chr(34) , vbTextCompare) _
& vbTab & InStr( 1, myString2, Chr(34)&Chr(34), vbTextCompare) _
& vbNewLine & myString4 _
& vbTab & InStr( 1, myString4, Chr(34) , vbTextCompare) _
& vbTab & InStr( 1, myString4, Chr(34)&Chr(34), vbTextCompare)
Output:
==>cscript //nologo 28778280.vbs
28778280.vbs: Using Double Quotes
none double quotes 0 0
with "double" quotes 1 6 0
with "double" quotes 2 6 0
twice ""double"" quotes 7 7

Adding quotes to a string in VBScript

I have this code:
a = "xyz"
g = "abcd " & a
After running it, the value of g is abcd xyz.
However, I want quotes around the value of a in g. After running the code, g should be abcd "xyz" instead.
How can I accomplish this?
You can escape by doubling the quotes
g="abcd """ & a & """"
or write an explicit chr() call
g="abcd " & chr(34) & a & chr(34)
You have to use double double quotes to escape the double quotes (lol):
g = "abcd """ & a & """"
I usually do this:
Const Q = """"
Dim a, g
a = "xyz"
g = "abcd " & Q & a & Q
If you need to wrap strings in quotes more often in your code and find the above approach noisy or unreadable, you can also wrap it in a function:
a = "xyz"
g = "abcd " & Q(a)
Function Q(s)
Q = """" & s & """"
End Function
The traditional way to specify quotes is to use Chr(34). This is error resistant and is not an abomination.
Chr(34) & "string" & Chr(34)
You can do like:
a="""xyz"""
g="abcd " & a
Or:
a=chr(34) & "xyz" & chr(34)
g="abcd " & a
I don't think I can improve on these answers as I've used them all, but my preference is declaring a constant and using that as it can be a real pain if you have a long string and try to accommodate with the correct number of quotes and make a mistake. ;)
I designed a simple approach using single quotes when forming the strings and then calling a function that replaces single quotes with double quotes.
Of course this approach works as long as you don't need to include actual single quotes inside your string.
Function Q(s)
Q = Replace(s,"'","""")
End Function
...
user="myself"
code ="70234"
level ="C"
r="{'User':'" & user & "','Code':'" & code & "','Level':'" & level & "'}"
r = Q(r)
response.write r
...
Hope this helps.
I found the answer to use double and triple quotation marks unsatisfactory. I used a nested DO...LOOP to write an ASP segment of code. There are repeated quotation marks within the string. When I ran the code:
thestring = "<asp:RectangleHotSpot Bottom=""" & bottom & """ HotSpotMode=""PostBack"" Left="""& left & """ PostBackValue=""" &xx & "." & yy & """ Right=""" & right & """ Top=""" & top & """/>"
the output was:
<`asp:RectangleHotSpot Bottom="28
'Changing the code to the explicit chr() call worked:
thestring = "<asp:RectangleHotSpot Bottom=""" & bottom & chr(34) & " HotSpotMode=""PostBack"" Left="""& left & chr(34) & " PostBackValue=""" &xx & "." & yy & chr(34) & " Right=""" & right & chr(34) & " Top=""" & top & chr(34) &"/>"
The output:
<asp:RectangleHotSpot Bottom="28" HotSpotMode="PostBack" Left="0" PostBackValue="0.0" Right="29" Top="0"/>

Resources