VBScript - How to encode Greek Characters in UTF8 as they appear incorrectly?

127 Views Asked by At

I have made a VBScript code to print Greek characters. The issue here is that the characters appear incorrectly, even after my attempt to encode in UTF-8.

I am posting my VBScript code below:

MsgBox("Θραύσεις Κρυστάλλων") 'The text here appears as weird unreadable symbols because of the current default encoding

Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type     = 2 'text
stream.Position = 0
stream.Charset  = "utf-8"

stream.WriteText "Θραύσεις Κρυστάλλων" 'here i supply the text again, to the Stream

stream.Flush
stream.Position = 0
stream.Type     = 1 'binary
stream.Read(3)      'skip BOM
utfStr = stream.Read
stream.Close
Set stream = Nothing

MsgBox(utfStr) 'Here I print the UTF8-encoded string from the stream. It appears different from the 1st messagebox, but still incorrectly

What could I be missing? Should I supply the Greek text via a text file instead, and avoid using variables?

Thanks in advance!

1

There are 1 best solutions below

1
LesFerch On

VBScript does not support UTF-8. It will handle UTF-8 as ANSI, so you can read and write UTF-8 files (since it will just be handled as a stream of bytes) but you cannot display the UTF-8 encoded characters correctly. You have two options:

Option 1

Save your script as UTF-16 LE

MsgBox will display the characters correctly.

Note that, just like any other VBS script, the message box dialog will be displayed using DPI virtualization on displays that are set higher than 100% scaling. This makes the display blurry.

enter image description here

Option 2

Embed your VBS script in an HTA saved as UTF-8

In this example, the HTA window is hidden by moving it off screen. If you wish to build a more sophisticated interface, then you could keep the HTA window on screen and use HTML/CSS to build your GUI.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=10">
<script language="VBScript">
Window.MoveTo 999999999,999999999
MsgBox "Θραύσεις Κρυστάλλων"
</script>
</head>
<body>
</body>
</html>

DPI virtualization is not used. The message box will display clearly and at the correct size, regardless of the display scale setting.

enter image description here