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
Related
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).
i am creating a .vbs file that should open access, and inside access a form call "Issue Details", but passing a parameter, meaning that if i have 10 issues in my "Issues" table a vbs file is created for each one and when clicked should open the right record(would be one ID for each record in the table). It is so far opening access and it is opening the form(Issue Details) but it is blank. What am i missing? Help, getting crazy here ... Check code below. The weird thing here is that if i double click it again it will refresh and open the right record without opening anymore windows..
How can i fix that? I dont want to do it twice :)
Public Sub sendMRBmail(mrbid)
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid
End Sub
Private Sub Create_Click()
On Error GoTo Err_Command48_Click
Dim snid As Integer
snid = Me.ID
Dim filename As String
filename = "S:\Quality Control\vbs\QC" & snid & ".vbs"
Dim proc As String
proc = Chr(34) & "sendMRBmail" & Chr(34)
Dim strList As String
strList = "On Error Resume Next" & vbNewLine
strList = strList & "dim accessApp" & vbNewLine
strList = strList & "set accessApp = createObject(" & Chr(34) & "Access.Application" & Chr (34)")" & vbNewLine
strList = strList & "accessApp.OpenCurrentDataBase(" & Chr(34) & "S:\Quality Control\Quality DB\Quality Database.accdb" & Chr(34) & ")" & vbNewLine
strList = strList & "accessApp.Run " & proc & "," & Chr(34) & snid & Chr(34) & vbNewLine
strList = strList & "set accessApp = nothing" & vbNewLine
Open filename For Output As #1
Print #1, strList
Close #1
Err_Command48_Click:
If Err.Number <> 0 Then
MsgBox "Email Error #: " & Err.Number & ", " & "Description: " & Err.Description
Exit Sub
End If
End Sub
Found the problem. Changed instruction below, adding acFormEdit to it and it worked:
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid, acFormEdit
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.
Have errors on Line 59, Char 1.... "The Interface is Unknown"
The intent is to popup a simple user input box and capture the results into a variable, and then into a text file, so a 3rd party application can read it. I am not very good at coding VBS but I made this work perfectly in Windows 7.
I am looking to make this (VBscript) work in Windows 8 too. But I don't want to learn how to script or code in another language. I don't want to rewrite the whole thing in Javascript or .NET or whatever.
Please let me know what Windows 8 is doing differently from Windows 7 sp1.
Thanks.
'=======================[ ASK Password ]========================================'
Option Explicit
Dim strUserID, strPassword
AskPassword
Sub AskPassword()
Dim htmlPwdCode, objCodeFile, objFileSysObj, objBrowser, strButton
Const FOR_WRITING = 2
Set objFileSysObj = CreateObject("Scripting.FileSystemObject")
htmlPwdCode = "<SCRIPT LANGUAGE=" & Chr(34) & "VBScript" & Chr(34) & ">" & Chr(13) & _
"Sub RunScript" & Chr(13) & _
" OKClicked.Value = " & Chr(34) & "OK"& Chr(34) & Chr(13) & _
"End Sub" & Chr(13) & _
"Sub CancelScript" & Chr(13) & _
" OKClicked.Value = " & Chr(34) & "Cancelled" & Chr(34) & Chr(13) & _
"End Sub" & Chr(13) & _
"Sub Default_Buttons" & Chr(13) & _
" If Window.Event.KeyCode = 13 Then" & Chr(13) & _
" btnOK.Click" & Chr(13) & _
" End If" & Chr(13) & _
"End Sub" & Chr(13) & _
"</SCRIPT>" & Chr(13) & _
"<BODY onkeypress='vbs:Default_Buttons'><center><font size=" & Chr(34) & "2" & Chr(34) & " face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
"User name: " & Chr(13) & _
"<input type=" & Chr(34) & "text" & Chr(34) & " name=" & Chr(34) & "UserName" & Chr(34) & " size=" & Chr(34) & "30" & Chr(34) & "><br>" & Chr(13) & _
"Password : </font><font face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
"<input type=" & Chr(34) & "password" & Chr(34) & " name=" & Chr(34) & "UserPassword" & Chr(34) & _
" size=" & Chr(34) & "30" & Chr(34) & "></font></p>" & Chr(13) & _
"<input type=" & Chr(34) & "hidden" & Chr(34) & " name=" & Chr(34) & "OKClicked" & Chr(34) & " size = " & Chr(34) & "20" & Chr(34) & ">" & Chr(13) & _
"<input id=" & Chr(34) & "btnOK" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
" type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & " OK " & Chr(34) & _
" name=" & Chr(34) & "ok_button" & Chr(34) & " onClick=" & Chr(34) & "RunScript" & Chr(34) & ">" & Chr(13) & _
"<input id=" & Chr(34) & "btnCancel" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
" type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & "Cancel" & Chr(34) & _
" name=" & Chr(34) & "cancel_button" & Chr(34) & " onClick=" & Chr(34) & "CancelScript" & Chr(34) & "></center></BODY>"
Set objCodeFile = objFileSysObj.CreateTextFile("LoginPrompt.html", True)
objCodeFile.Write htmlPwdCode
objCodeFile.Close
Set objCodeFile = Nothing
Set objBrowser = CreateObject("InternetExplorer.Application")
With objBrowser
.Height = 200
.Width = 400
.Top = 200
.Left = 300
.StatusBar = True
.Toolbar = False
.Resizable = False
.Navigate CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\LoginPrompt.html"
.Visible = True
End With
Do Until objBrowser.ReadyState = 4
'wait till page loads'
Loop
Do While objBrowser.Document.Body.All.OKClicked.Value = ""
Wscript.Sleep 50
Loop
strUserID = objBrowser.Document.Body.All.UserName.Value
strPassword = objBrowser.Document.Body.All.UserPassword.Value
strButton = objBrowser.Document.Body.All.OKClicked.Value
'''''''''''''''''''''''
Dim objFSO, strFile, objFile
Const ForWriting = 2
Const OpenAsASCII = 0
Const CreateIfNotExist = True
' Specify output file.
strFile = "C:\TEMP\MEX\UN.txt"
' Open the file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, _
ForWriting, CreateIfNotExist, OpenAsASCII)
' write to file.
objFile.WriteLine strUserID
' Clean up.
objFile.Close
'''''''''''''''''''''''''''
Dim objFSO2, strFile2, objFile2
Const ForWriting2 = 2
Const OpenAsASCII2 = 0
Const CreateIfNotExist2 = True
' Specify output file.
strFile2 = "C:\TEMP\MEX\PW.txt"
' Open the file.
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO2.OpenTextFile(strFile2, _
ForWriting2, CreateIfNotExist2, OpenAsASCII2)
' write to file.
objFile2.WriteLine strPassword
' Clean up.
objFile2.Close
'''''''''''''''''''''''''''
objBrowser.Quit
If strButton = "Cancelled" Then
MsgBox "Operation cancelled, script will now exit!"
Wscript.Quit
Else
'Credentials accepted for further processing
End If
objFileSysObj.DeleteFile "LoginPrompt.html", True
Set objBrowser = Nothing
Set objFileSysObj = Nothing
End Sub
'=======================[ GOT Password ]========================================'
Line 59: Do Until objBrowser.ReadyState = 4
The problem, according to MSDN: ReadyState Property Example (VBScript), is that the "ReadyState" feature has been removed.
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"/>