VBscript Error "Expected Expression"
I am working on a vbscript program and I have an "Expected Expression" error. I cannot find the error. I have seen some examples of this error, but they did not work for me.
I am new to vbscript.
Here is the code.
Sub SetText(tx, lw)
Dim t, l, r, a
t = -1
l = Len(tx)
r = ""
a = 0
While t < l
t = t + 1
a = Asc(Mid(tx,t,1))
If a >= 160 or a=60 or a=62 or a=38 or a=34 or a=39 or a=32 Then
If a = 32 Then
r = r + " "
Else
r = r + "&#" + Cstr(a) + ";"
End If
Else
r = r + Mid(tx,t,1)
End If
End While 'The error occurs at the beginning of this statement.'
If Not lw Then
r = "<pre>" + r + "</pre>"
End If
r = "<div style='width:auto; height:auto;'>" + r + "</div>"
objExplorer.document.body.innerHTML = r
End Sub
source to share
While ... End While
? I don't think this is correct.
I think the syntax is:
While counter > 0
...
Wend
Try this instead, it has some other improvements (I'm pretty sure it Mid
uses base 1 and not base 0 - unless changed For t = 1 to Len (tx)
to For t = 0 to Len (tx) - 1
):
Sub SetText (tx, lw)
Dim t, r, c, a
'Standard prefix and optional pre tag.'
r = "<div style='width:auto; height:auto;'>"
If Not lw Then
r = r + "<pre>"
End If
'Process each character in string.'
For t = 1 to Len (tx)
'Get character and code.'
c = Mid (tx,t,1)
a = Asc (c)
'Change "character" if it is one of the special ones.'
If a = 32 Then
c = " "
Else
If a >= 160 or a = 60 or a = 62 or a = 38 or a = 34 or a = 39 Then
c = "&#" + Cstr (a) + ";"
End If
End If
'Add "character" to result (it may be a string at this point).'
r = r + c
Next
'Optional pre tag and standard suffix.'
If Not lw Then
r = r + "</pre>"
End If
r = r + "</div>"
'Inject into page.'
objExplorer.document.body.innerHTML = r
End Sub
I haven't tested this completely (well, actually, actually), so let me know if there is a problem (or just revert the original solution by replacing End While
with Wend
and possibly changing the range t
for base-1 Mid
).
source to share
See also: http://www.w3schools.com/vbscript/vbscript_looping.asp
paxdiablo's answer is right - you are not using End While
in VBScript. Your code should look like this:
Sub SetText(tx, lw)
Dim t, l, r, a
t = -1
l = Len(tx)
r = ""
a = 0
While t < l
t = t + 1
a = Asc(Mid(tx,t,1))
If a >= 160 or a=60 or a=62 or a=38 or a=34 or a=39 or a=32 Then
If a = 32 Then
r = r + " "
Else
r = r + "&#" + Cstr(a) + ";"
End If
Else
r = r + Mid(tx,t,1)
End If
Wend '<---'
If Not lw Then
r = "<pre>" + r + "</pre>"
End If
r = "<div style='width:auto; height:auto;'>" + r + "</div>"
objExplorer.document.body.innerHTML = r
End Sub
The last thing I checked is its outdated management structure and is generally discouraged. Your code should probably look like this:
Sub SetText(tx, lw)
Dim t, l, r, a
t = -1
l = Len(tx)
r = ""
a = 0
Do While t < l
t = t + 1
a = Asc(Mid(tx,t,1))
If a >= 160 or a=60 or a=62 or a=38 or a=34 or a=39 or a=32 Then
If a = 32 Then
r = r + " "
Else
r = r + "&#" + Cstr(a) + ";"
End If
Else
r = r + Mid(tx,t,1)
End If
Loop
If Not lw Then
r = "<pre>" + r + "</pre>"
End If
r = "<div style='width:auto; height:auto;'>" + r + "</div>"
objExplorer.document.body.innerHTML = r
End Sub
In fact, there are probably more changes, but not familiar with the context of this code as far as I suggest.
source to share