Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delcare global const unicode string

Status
Not open for further replies.

cheer8923

Programmer
Aug 7, 2006
230
US
How can I declare unicode string constant in VBA?

e.g.

Private Const Space As String = 0x3000

This won't work as the compiler won't recognize 0x3000 as hexidecimal for a unicode.

thanks!

 
Short answer:
VBA does not do Unicode strings

Longer Answer:
VBA strings are all unicode deep beneath the surface of the IDE, but they are always presented to the VBA programmer as pure ASCII strings

Summary
You cannot directly insert or extract Unicode from VBA strings. The normal workaround is to use a byte array or to treat the string as a byte buffer
 
I see the macro uses ChrW(20000) alike to assign unicode characters. Is this the only way to do so?
 
The way to use hexadecimal literals is to prefix them with &H (rather than 0x). They will not however be coerced to characters in the way you are trying, so, in your example, if you coded:
[tt] Private Const Space As String = &H3000[/tt]
.. although it would compile, the string would have the value "12389".

There is no mechanism for entering non-ANSI characters directly in the editor, and the way to get them into strings is to use the [blue][tt]ChrW[/tt][/blue] function, but functions cannot be used outside procedures, nor can they be used in the declaration of Constants. So, in this instance, you must use a variable, rather than a constant, and you must initialise it in a procedure.

I must disagree with Mike. All VBA Strings are Unicode strings, not deep within, but everywhere. There is some conversion goes on behind the scenes when interfacing with external code, but at that point they are no longer VBA strings; within VBA you always have wide characters. If you explicitly use ANSI characters you will get unexpected results - about the only place you come across this is with the [blue][tt]Chr[/tt][/blue] and [blue][tt]Asc[/tt][/blue] functions.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
>All VBA Strings are Unicode strings

They are BSTRs, which are not quite the same thing
 
> They are BSTRs, which are not quite the same thing

That is a technicality - and that is beneath the surface. Because VBA (and, more generally, Typed languages) isolate the programmer from memory, it is necessary for control information about variables to be maintained. All that a BSTR is, is a unicode string plus that control information.

All normal interactions with strings in VBA are with unicode characters, and none are with ANSI (or ASCII) characters, and you most certainly can insert and extract unicode characters to and from VBA strings, without messing about with byte arrays.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top