-
Oct 22nd, 2021, 05:58 AM
#1
Thread Starter
New Member
-
Oct 22nd, 2021, 06:56 AM
#2
Re: Weird characters when running vba codes
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Oct 22nd, 2021, 07:17 AM
#3
Thread Starter
New Member
Re: Weird characters when running vba codes
Originally Posted by Zvoni
Codepage?
Its not just about codepage (I think so), because I'm facing same issue in all apps on windows.
In access forms and vba code, excel forms and sheets, vb6 forms and code. VS .net editor aswell
I performed changes on Regional settings but this issue happens with no motivation, was working fine and just crashed.
-
Oct 22nd, 2021, 07:42 AM
#4
Re: Weird characters when running vba codes
Maybe this applies: https://www.ghisler.ch/board/viewtopic.php?t=58135
It's about the option in regional settings: "Beta: Use Unicode UTF-8 for worldwide language support".
This must be unchecked?
If you check this option, Windows will use codepage 65001 (Unicode UTF-8) instead of the local codepage like 1252 (Western Latin1) for all plain text files. The advantage is that text files created in e.g. Russian locale can also be read in other locale like Western or Central Europe. The downside is that ANSI-Only programs (most older programs) will show garbage instead of accented characters.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Oct 23rd, 2021, 12:35 PM
#5
Re: Weird characters when running vba codes
In the MS world:
ASCII - 7 bit
ANSi - 8 bit
Unicode - 16 bit
VB stores string information as Unicode. The problem is that some characters above 127 are considered Unicode characters and get converted in the background by the operating system. VB was originally designed for ASCII and cannot read these characters, so the result is a "?". I threw this together to demonstrate.
Code:
Option Explicit
Private Const CP_UTF8 = 65001
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Function GetbSize(bArray() As Byte) As Long
On Error GoTo GetSizeErr
GetbSize = UBound(bArray) + 1
Exit Function
GetSizeErr:
GetbSize = 0
End Function
Private Sub DebugPrintByte(sDescr As String, bArray() As Byte)
Dim lPtr As Long
On Error GoTo Done
Debug.Print sDescr & ":"
For lPtr = 0 To UBound(bArray)
Debug.Print Right$("0" & Hex$(bArray(lPtr)), 2) & " ";
If (lPtr + 1) Mod 16 = 0 Then Debug.Print
Next lPtr
Done:
Debug.Print
End Sub
Private Function StrToUtf8(strInput As String) As Byte()
Dim nBytes As Long
Dim abBuffer() As Byte
If Len(strInput) < 1 Then Exit Function
' Get length in bytes *including* terminating null
nBytes = WideCharToMultiByte(CP_UTF8, 0&, ByVal StrPtr(strInput), -1, 0&, 0&, 0&, 0&)
' We don't want the terminating null in our byte array, so ask for `nBytes-1` bytes
ReDim abBuffer(nBytes - 2) ' NB ReDim with one less byte than you need
nBytes = WideCharToMultiByte(CP_UTF8, 0&, ByVal StrPtr(strInput), -1, ByVal VarPtr(abBuffer(0)), nBytes - 1, 0&, 0&)
StrToUtf8 = abBuffer
End Function
Private Function Utf8ToStr(abUtf8Array() As Byte) As String
Dim nBytes As Long
Dim nChars As Long
Dim strOut As String
' Catch uninitialized input array
nBytes = GetbSize(abUtf8Array)
If nBytes <= 0 Then Exit Function
' Get number of characters in output string
nChars = MultiByteToWideChar(CP_UTF8, 0&, VarPtr(abUtf8Array(0)), nBytes, 0&, 0&)
' Dimension output buffer to receive string
strOut = String(nChars, 0)
nChars = MultiByteToWideChar(CP_UTF8, 0&, VarPtr(abUtf8Array(0)), nBytes, StrPtr(strOut), nChars)
Utf8ToStr = Left$(strOut, nChars)
End Function
Private Sub Command1_Click()
Dim sTmp As String
Dim bTmp() As Byte
sTmp = "ABC_DEF"
CopyMemory ByVal StrPtr(sTmp) + 6, 149, 1
ReDim bTmp(LenB(sTmp))
CopyMemory bTmp(0), ByVal StrPtr(sTmp), LenB(sTmp)
'bTmp(6) = &H95 '149
DebugPrintByte "Original String", bTmp
Debug.Print sTmp
bTmp = StrToUtf8(sTmp)
DebugPrintByte "ASCII String", bTmp
sTmp = Utf8ToStr(bTmp)
Debug.Print sTmp
End Sub
Results:
Original String:
41 00 42 00 43 00 95 00 44 00 45 00 46 00 00
ABC?DEF
ASCII String:
41 42 43 C2 95 44 45 46
ABC?DEF
You can see that on my system, 00 95 is converted to C2 95 because it is considered Unicode.
J.A. Coutts
-
Oct 23rd, 2021, 02:08 PM
#6
Re: Weird characters when running vba codes
Originally Posted by guergolet
When I use crh(149) ...
... then you're working with the ANSI-function -
and are willing to make proper text-rendering depending "on a bet"
(the bet being, that hopefully the target-system has the same locale as your dev-system).
VB6-Strings are BStrings - and "natively" (by default) already "Wide" (not 8Bit ANSI).
So, when you want to concatenate some special chars into existing (V)BStrings,
you can remain on the safe side, by *avoiding* any risky ANSI-conversions,
in choosing the proper Character-Conversion-Function, which in this case is:
ChrW$(8226) '<-- to hand out your bullet-char • ...as a proper WideChar-String
HTH
Olaf
-
Oct 23rd, 2021, 03:58 PM
#7
Re: Weird characters when running vba codes
Originally Posted by couttsj
The problem is that some characters above 127 are considered Unicode characters and get converted in the background by the operating system. VB was originally designed for ASCII and cannot read these characters, so the result is a "?".
This is either an upside-down or inside-out view of the world (or both).
VB6 source code is ANSI, but only the ASCII subset is safe. This should be obvious, because ANSI encoding is always locale-relative.
When String literals are compiled the ANSI source gets compiled as Unicode characters. This has nothing to do with "the operating system" at all. It occurs relative to the current locale and codepage.
If you try to display a VB String value you always risk a loss of fidelity for characters outside the ASCII subset. That's because Debug.Print and many controls supplied in and with VB6 had not been provided as Unicode-aware versions yet before VB was killed off after VB6. The "?" glyphs (or in some fonts an "empty box" glyph) are what get rendered for characters outside the ANSI encoding in use. Depending on where and how the conversion occurs they may get turned into actual "?" characters or the "notdef" glyph.
https://docs.microsoft.com/en-us/typ...ype/spec/recom
Glyph 0: the .notdef glyph
Glyph 0 must be assigned to a .notdef glyph. The .notdef glyph is very important for providing the user feedback that a glyph is not found in the font. This glyph should not be left without an outline as the user will only see what looks like a space if a glyph is missing and not be aware of the active font’s limitation.
It is recommended that the shape of the .notdef glyph be either an empty rectangle, a rectangle with a question mark inside of it, or a rectangle with an “X”. Creative shapes, like swirls or other symbols, may not be recognized by users as indicating that a glyph is missing from the font and is not being displayed at that location.
-
Oct 23rd, 2021, 10:05 PM
#8
Re: Weird characters when running vba codes
The characters that do not interpreted correctly are &H80 to &HA0 (128 - 160). Of these, 4 get interpreted as a space and the rest as a ?. 161 to 255 get interpreted correctly.
J.A. Coutts
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|