How to set Japanese characters(UTF-8) in VBScript variables - vbscript

I have a script where I need to set some Japanese text to a variable. But since vbscript is not supporting japanese texts, it get converted to some garbled text like トコジャパンã‹ã‚‰ã®æ–°è¦æ³¨æ–‡. My actual japanese text is トコジャパンからの新規注文.
My script will look like below
dim emlObj
set emlObj = CreateObject("EMailObject")
emlObj.Subject = "Train - New Orders From Costco Japan | コストコジャパンからの新規注文"
emlObj.Body = "Some japanese body text"
emlObj.Send()
I do not have any other options like storing this text in file or db and process in some other scripts as of now. since this script will be used by our customers and they will set their expected email body text. We'll use them for sending it as a mail.
I've also tried ADODB.Steam but that works only when reading the text from file.
Can someone please suggest a way to set the japanese text in vbscript?.
Edit:
To put a clarity on what i really needed.
I want to hard-code a japanese text to a variable in VBScripts.

You can use this solution:
Dim objStream, body, subject
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.WriteText "Subject in Japanese"
objStream.SaveToFile "C:\Subject.txt", 2
objStream.Close
objStream.Open
objStream.WriteText "Body in Japanese"
objStream.SaveToFile "C:\body.txt", 2
objStream.Close
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile "C:\Body.txt"
body = objStream.ReadText()
objStream.Close
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile "C:\Subject.txt"
subject = objStream.ReadText()
objStream.Close
' Now body is stored in body variable and subject is in subject now do anything with them

IMPORTANT: Since you are not using ADODB.Stream to load your content, then be sure to save your actual VBScript file as UTF-16 LE encoding. Your script may optionally benefit from a Unicode byte-order-mark (BOM) header, as well -- depending upon how the VBScript engine and EmailObject respond.
Once the VBScript file is in the proper encoding, this script works fine for me:
With CreateObject("CDO.Message")
With .Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.somemailserver.net"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
.BodyPart.Charset = "utf-8"
.BodyPart.ContentTransferEncoding = "base64"
.TextBody = "コストコジャパンからの新規注文"
.TextBodyPart.Charset = "utf-8"
.HTMLBody = "<p>コストコジャパンからの新規注文</p>"
.HTMLBodyPart.Charset = "utf-8"
.From = "me#somemailserver.net"
.To = "you#somemailserver.net"
.Subject = "Train - New Orders From Costco Japan | コストコジャパンからの新規注文"
.Send()
End With
Note: I am using CDO.Message above, not EmailObject.
Hope this helps.

Related

vbscript ADO stream charset output

My webhook is receiving POST request (aplication/JSON) from a 360Dialog (whatsapp) API with escaped Unicode characters like this: \u05db\u05e0\u05e1\u05d2\u05db\u05d9. It should be Hebrew letters.
I'm trying to decode that using JavaScript runat server but seems like it is not changing. I found a potential solution in this question's solution but it still saves the un-escaped Unicode into the database.
<script language="javascript" runat="server">
URL = {
decode : function(s){return decodeURIComponent(s.replace(/\+/g, " "))}
}
</script>
<%
rs("smstext")=URL.decode(body2)
%>
The POST request is coming from 360dialog (a Whatsapp API) and hitting my webhook.
the request sends an application/json POST with information of incoming Whatsapp messages.
It seems the POST itself already has the Hebrew in it as \u05e0\u05e1\u05d9\u05d5\u05df i guess i need to figure out how to set the charset for that?
also, this unanswered question seems like my same problem.
I am trying to convert a request.BinaryRead into utf-8.
the output in the database is this: \u05e0\u05e1\u05d9\u05d5\u05df instead of נסיון
I am probably misunderstanding something as the output is not what I expected.
my code is:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include virtual="/include/aspjson.asp" -->
<%
If Request.TotalBytes > 0 Then
Dim lngBytesCount
lngBytesCount = Request.TotalBytes
body = BytesToStr(Request.BinaryRead(lngBytesCount))
Set db = CreateObject("ADODB.Connection")
db.Open "DSN=xxx"
set rs = Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM log_sms", db, 3, 3
rs.addnew
rs("smstext")=body
rs.update
rs.close
End if
Function BytesToStr(bytes)
Dim Stream
Set Stream = Server.CreateObject("Adodb.Stream")
Stream.Type = 1 'adTypeBinary
Stream.Open
Stream.Write bytes
Stream.Position = 0
Stream.Type = 2 'adTypeText
Stream.Charset = "utf-8"
BytesToStr = Stream.ReadText
Stream.Close
Set Stream = Nothing
End Function
%>
If I replace rs("smstext")=body with rs("smstext")="נסיון", the value in the database is saved correctly.
The approach is sound the problem is because the text is escaped in the JSON body you will need to unescape those characters before saving the content to a database.
Would recommend using this particular JSON Parser as it will automatically handle unescaping the characters for you.
Useful Links
Classic ASP Convert Latin Characters to Unicode Escape Strings *(Contains useful information about escaping, which can help with the unescaping aspect).
The solution (thanks to #user692942 comments):
I replaced the aspJSON library I was using with rcdmk/aspJSON. It takes care of decoding escaped characters already and simplified the process.
Since the POST request to my webhook page is in application/JSON a JSON library is required anyways.
to my little understanding, i must use BinaryRead to fetch the data from such a request. And thus have to convert from byte to str.
the working code is as follows:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001" LCID="1037"%>
<!--#include virtual="/wa/aspjson.asp" -->
<%
If Request.TotalBytes > 0 Then
Dim lngBytesCount
lngBytesCount = Request.TotalBytes
jsonbyte = Request.BinaryRead(lngBytesCount)
jsonStr = BytesToStr(jsonbyte)
Set JSON = New JSONobject
Set jsn = JSON.Parse(jsonStr)
Set contact = jsn.value("contacts")(0)
set profile = contact.value("profile")
profname = profile.value("name")
wa_id = contact.value("wa_id")
Set message = jsn.value("messages")(0)
from = message.value("from")
id = message.value("id")
timestamp = message.value("timestamp")
mtype = message.value("type")
set text = message.value("text")
body = text.value("body")
End If
Set db = CreateObject("ADODB.Connection")
db.Open "DSN=xxx"
set rs = Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM log_sms", db, 3, 3
rs.addnew
rs("sent")=DateAdd("s", timestamp, DateSerial(1970,1,1))
rs("from")=from
rs("to")="whatsapp"
rs("smstext")=body
rs("result")="received"
rs("msgid")=id
rs("snr")="r"
rs("type")=mtype
rs.update
rs.close
Function BytesToStr(bytes)
Dim Stream
Set Stream = Server.CreateObject("Adodb.Stream")
Stream.Type = 1 'adTypeBinary
Stream.Open
Stream.Write bytes
Stream.Position = 0
Stream.Type = 2 'adTypeText
Stream.Charset = "utf-8"
BytesToStr = Stream.ReadText
Stream.Close
Set Stream = Nothing
End Function

Encoding issue when creating MS Word file using VBScript

I'm trying to write a script in VBScript which should open Microsoft Word and write down some text.
The script works as expected as long as the text I'm writing is in English.
However, when the text is in Hebrew or in Chinese I only get Gibberish in MS Word.
I tried to save the script file as UTF-8, but I can no longer run it after this change.
I also tried to wrap it so it will be a wsf script and it didn't worked either.
Couldn't find any other suggestion on Google.
Here is the script (This time I'm trying to write the word "שלום" in Hebrew).
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.TypeText "שלום"
When I run this script, it opens MS word and write down "ùìåí" instead of "שלום".
You can save a script file with the following encoding:
ANSI. Only 256 chars can be used: 0..127 is standard ASCII, and upper part depends on locale you have chosen in system settings, or overridden by SetLocale().
Unicode (UCS-2 or UTF-16, Little Endian). It works if saved with BOM, or without BOM. There is 1 112 064 available chars. In my opinion it is the easiest way for you to get your script to work. But file size increased by 2 times.
UTF-8. Encodes any symbol in Unicode code space. Script can be ran only if saved without BOM.
UTF-8 as .wsf file with first tag <?XML version="1.0" encoding="UTF-8"?>
ANSI, but put all strings like WScript.Echo ChrW(1513) & ChrW(1500) & ChrW(1493) & ChrW(1501).
Notepad++ and Notepad2 are handy to clearly set the necessary encoding.
Regarding item 3. Generally, Windows Script Host is unable to run script file in UTF-8 encoding with BOM, and recognizes each byte of UTF-8-encoded file without BOM as ANSI-encoded char, while downloading the file into memory. I can suggest a work-around that allows to rectify incorrectly recognized chars being contained in variables, but you know, Unicode is a better way. Here is example:
s = "שלום"
WScript.Echo s ' wrong encoding
r = FixChars(s)
WScript.Echo r ' שלום
Function FixChars(s)
Dim r, p
r = ""
For p = 1 To Len(s)
r = r & ChrB(Asc(Mid(s, p, 1)))
Next
With CreateObject("ADODB.Stream")
.Type = 2
.Mode = 3
.Charset = "Unicode" ' HKLM\SOFTWARE\Classes\MIME\Database\Charset
.Open
.WriteText r
.Position = 0
.Charset = "UTF-8"
r = .ReadText
.Close
End With
Do While LeftB(r, 2) = ChrB(&HFD) & ChrB(&HFF)
r = MidB(r, 3)
Loop
FixChars = r
End Function
You shouldn't change locale via SetLocale() from script start till FixChars() completion, otherwise it will give an error.And the following code is an example for item 4:
<?XML version="1.0" encoding="UTF-8"?>
<job>
<script language="VBScript">
<![CDATA[
WScript.Echo "שלום"
]]>
</script>
</job>

VBS Readline - using instr(), to match data whilst ignoring extra spaces

I'm trying to find a way to enhance the reliability of my script. It already works but can be thrown off with a simple extra space in the imported text file.
So I'd like to change my script to Readline if I can find a way to do something like:
Example of text in the .txt file:
FLIGHTS OVER TUSKY PLEASE FILE:
AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT
FLIGHTS OVER EBONY PLEASE FILE:
AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT
I know the following doesn't work but if there was a simple modification this would be good.
set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("C:\Downloads\software\putty.exe -load "testing")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt")
strLine = objFile.ReadAll
If InStr(strLine1, "OVER TUSKY PLEASE") and InStr(strLine2, "BAYYS..PUT..DIRECT") Then
trans307="TUSKY"
ind306="4"
WHAT I'M USING NOW:
I edit the text file in notepad++ to FIND & REPLACE "\n" with "" and "\r" with " " and then it's all one text string and I search for strings within that string.
If InStr(strLine, "FLIGHTS OVER TUSKY PLEASE FILE: AT OR WEST OF A LINE ..RBV..LLUND..BAYYS..PUT..DIRECT") _
or InStr(strLine, "FLIGHTS OVER TUSKY PLEASE FILE: AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT...DIRECT") Then
trans308C="TUSKY"
ind308C="4"
Problem: If the creators of the text file put another space " " anywhere in this line "AT OR WEST OF A LINE RBV..LLUND..BAYYS..PUT..DIRECT" the script will not identify the string. In the above example I have had to create another or InStr(strLine, "") statement with an extra space or with a couple of dots.
UPDATE:
I will try something like:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt")
strLine1 = objFile.Readline(1)
strLine2 = objFile.Readline(2)
If InStr(strLine1, "FLIGHTS OVER TUSKY") and InStr(strLine2, "RBV..LLUND..BAYYS..PUT..DIRECT") Then
trans1="TUSKY"
ind1="4"
and see if I can get that to read 2 lines at a time, and loop through the text file.
If you're scared of regex and looking for an alternative, you could create a clunky function to add to your script. Based on your samples, it would seem that fullstops are also never normally used for normal purposes and tend to represent spaces. (I would recommend using Regex instead!)
Using these presumptions, you could create a clunky function like this, that looks for fullstops, and converts them to spaces, removing extra spaces.. Obviously, this relies heavily on your input source files not changing too much - you really should be using a regex to work this stuff out properly.
You could test for the basic expected results using something like the function below.
For example say you had a line of text set in firLine with multiple spaces or fullstops, the function would recognize this:
firLine = "THIS.IS.A.TEST..YOU...SEE MULTIPLE SPACES"
if instr(sanitize(firLine),"THIS IS A TEST YOU SEE MULTIPLE SPACES") then
wscript.echo "Found it"
End If
Here's the clunky function that you could just paste at the end of your script:
Function sanitize(srStr)
Dim preSanitize, srC, spaceMarker
preSanitize = ""
for srC = 1 to len(srStr)
if mid(srStr, srC, 1) = "." then
preSanitize = preSanitize & " "
else
preSanitize = preSanitize & mid(srStr, srC, 1)
End If
spaceMarker = false
sanitize = ""
for srC = 1 to len(preSanitize)
If mid(preSanitize, srC, 1) = " " then
if spaceMarker = false then
sanitize = sanitize & mid(preSanitize, srC, 1)
spaceMarker = true
End If
else
sanitize = sanitize & mid(preSanitize, srC, 1)
spaceMarker = false
End If
Next
End Function
InStr() is a good tool for checking whether a strings contains a fixed/literal string or not. To allow for variation, you should use Regular Expressions (see this or that).
First of all, however, you should work on your specs. Describe in plain words and with some samples what you consider (not) to be a match.
E.g.: A string containing the words "FLIGHTS", "OVER", and "TUSKY" in that order with at least one space in between is a match - "FLIGHTS OVER TUSKY", "FLIGHTS OVER TUSKY"; "FLIGHTS OVER TUSKANY" is a 'near miss' - what about "AIRFLIGHTS OVER TUSKY"?
GREAT NEWS! I finally figured out how to do this.
Here is a snippet from "Entries1.txt"
FLIGHTS OVER BRADD KANNI PLEASE FILE:
VIA J174.RIFLE..ACK..DIRECT
OR RBV.J62.ACK..DIRECT
FLIGHTS OVER KANNI WHALE PLEASE FILE:
VIA J174.RIFLE..ACK..DIRECT OR
FLIGHTS OVER WHALE PLEASE FILE:"
ETC, ETC
set WshShell = WScript.CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("C:\Users\AW\Desktop\Entries1.txt")
Do until objFile.AtEndOfStream
firLine = objFile.ReadLine
If InStr(firLine, "FLIGHTS OVER KANNI WHALE PLEASE") Then
secLine = objFile.ReadLine
If InStr(secLine, "J174.RIFLE..ACK..DIRECT") Then
'I'm going to change the below once I piece it all together.
WScript.Echo "works"
Else WScript.Echo "Not found"
'cut, paste and modify all my "IF" statements below
End If
End If
loop

Foreign character encoding issue with CSV download

I am using UTF8 encoding for ASP pages. I want to implement a CSV download functionality for my website. Below is my response setup
With Response
.Buffer = true
.Expires = 0
.Clear
.Charset = "UTF-8"
.CodePage = 65001
.AddHeader "Content-Type", "text/html;charset=UTF-8"
.AddHeader "content-encoding", "UTF-8"
.AddHeader "content-disposition", "attachment; filename=""AgedDebtors.csv"""
End With
Encoding is set for response headers still i am getting encoded foreign characters in resulting CSV. Tried few things for this but was not able to get it right. Please let me know what i am missing here
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 2 'text
stream.Position = 0
stream.Charset = "utf-8"
stream.WriteText str
you may want to save the converted stream into a new file, so write this
stream.SaveToFile filename, 2
And to close the stream use
stream.Close

Script queries database but doesn't get unicode characters

I have a small vbscript file that queries a mysql database and returns a recordset which I then send to excel.
The problem is that the recordset does not return russian characters, it only returns "?" for each character.
My code is
dim adoConn
dim adoRS
dim n
set adoConn = Createobject("ADODB.Connection")
set adoRS = Createobject("ADODB.Recordset")
adoConn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=server1;DATABASE=dbtest;USER=root;PASSWORD=daveeades;OPTION=3;"
adoRS.ActiveConnection = adoConn
n=1
if adoConn.errors.count = 0 then
'now get all necessary text comments
adoRS.Open "SELECT `tbllaunchdata`.`fldResponse` FROM `tbllaunchdata`"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
While (Not adoRS.EOF)
objExcel.Cells(n, 1).Value = adoRS("fldResponse")
n = n + 1
adoRS.Movenext()
Wend
end if
adoRS.close
set adoRS=nothing
adoConn.close
set adoConn=nothing
Could anyone please help me with this, I just can't get the unicode characters showing in excel.
Many thanks
Dave
There are many possible culprits.
To start with an easy check: Start - Programs - MS Office Tools - Ms
Office Languge Settings => Is Russian enabled?
For completeness: can you use "show variables" or "\s" to make sure of the MySQL character_set_client/connection/database/... and the collations? (I can do tests with a strict utf8 config (on a linux machine)
WRT comment: can you do a test like this
Air! code:
Dim sTest : sTest = "expected russian string"
adoRS.Open "SELECT `tbllaunchdata`.`fldResponse` FROM `tbllaunchdata`"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(0, 1).Value = adoRS("fldResponse")
objExcel.Cells(1, 1).Value = sTest
objExcel.Cells(2, 1).Value = CStr( sTest = adoRS("fldResponse") )
No thanks to me: looks like the the real important item should be:
use up-to-date software components!
Hiii .. I have the sameproblem.. But i can get the currect data from DB. But while displaying it on excel cell it shows as ????...If u got any solution please let me know.. To get pass Unicode data to Ms sql server we need to Use NVarchar Data Type... with adVarWChar..
Regards,
Liyo Jose.

Resources