-1

I cannot concatenate 2 strings. The first string is the result of StrConv(), and the second is just a simple string like "K".

These do not work:

MsgBox StrConv("O", vbUnicode) & "K"
MsgBox StrConv("O", vbUnicode) + "K"
MsgBox CStr(StrConv("O", vbUnicode)) & "K"
Dim g(1) As String
g(0) = CStr(StrConv("O", vbUnicode))
g(1) = CStr("K")
MsgBox Join(g(), vbNullString)

The expected result is "OK"

This is a simplified version of StrConv which is enough to see that concatenation is not working. However, my real case would be StrConv(ChrW$(240), 64, 1063)

ZygD
  • 22,092
  • 39
  • 79
  • 102
  • Msgbox is not unicode-capable I think. Try `Debug.Print` as a comparison... – Tim Williams Oct 14 '15 at 20:48
  • @TimWilliams has reason, but is rare that with left function, works, maybe because that left function Inherited from excel cells and all his capabilities. I want an explanation :) . – Juan Ruiz de Castilla Oct 14 '15 at 20:52
  • @TimWilliams - I will need `MsgBox`, because it asks for a response from users. Using the same `StrConv` line, `Debug.Print` shows different results on 2 separate machines, while `MsgBox` shows the same letter that I need. – ZygD Oct 14 '15 at 20:55
  • Both answer below are completely wrong. `"O"` is already Unicode, `StrConv("O", vbUnicode)` is [wrong](https://stackoverflow.com/a/14292880/11683). Your actual issue is that **`MsgBox` does not support Unicode** -- see [ms access - How do I display a messagebox with unicode characters in VBA? - Stack Overflow](https://stackoverflow.com/questions/55210315/how-do-i-display-a-messagebox-with-unicode-characters-in-vba/55210316?noredirect=1#comment122613435_55210316) – user202729 Oct 05 '21 at 12:25

2 Answers2

4

From: http://blog.nkadesign.com/2013/vba-unicode-strings-and-the-windows-api/

#If VBA7 Then
    Public Declare PtrSafe Function MessageBoxW Lib "user32" _
                                    (ByVal hwnd As LongPtr, _
                                     ByVal lpText As String, _
                                     ByVal lpCaption As String, _
                                     ByVal wType As Long) As Long
#Else
    Public Declare Function MessageBoxW Lib "user32" _
                    (ByVal hwnd As Long, _
                                 ByVal lpText As String, _
                                 ByVal lpCaption As String, _
                                 ByVal wType As Long) As Long
#End If

Sub tt()


    Dim g(1) As String, r
    g(0) = CStr(StrConv("O", vbUnicode))
    g(1) = "K"
    r = Join(g(), vbNullString)

    MessageBoxW 0, r, StrConv("test", vbUnicode), 0



End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • I like really like the approach! It works with "OK". But for my real data `StrConv(ChrW$(240), 64, 1063)` returns `ð` instead of `š` in the MsgBox. I need `š` – ZygD Oct 14 '15 at 21:12
  • 1
    I don't have anything else to offer - this is really not an area where I'm knowledgeable... – Tim Williams Oct 14 '15 at 22:58
  • These unicode things are really complicated for me too. I come back to them time to time, but I still cannot build some lasting knowledge here. I really value your input. There is another machine where I have the most of problems of this kind. So maybe I would need to return to your solution and build something on top of it, if the `Left` method failed on that machine to return the letters I need. The referenced article is really nice too. Thanks! – ZygD Oct 15 '15 at 06:21
  • The link is dead now. Web archive still have it https://web.archive.org/web/20180823033309/http://blog.nkadesign.com/2013/vba-unicode-strings-and-the-windows-api/ (actually that blog post only explain how to display message box with Unicode character. Just use [ms access - How do I display a messagebox with unicode characters in VBA? - Stack Overflow](https://stackoverflow.com/questions/55210315/how-do-i-display-a-messagebox-with-unicode-characters-in-vba/) instead) – user202729 Oct 05 '21 at 12:18
2

There is an extra something being placed in the first string. This is what is not allowing the second part to be seen

Use

MsgBox left(StrConv("O", vbUnicode),1) & "K"

After some research it is placing a null character after each actual character.

So if you want more than one character at a time use this

Msgbox Replace(StrConv("O", vbUnicode),chr(0),"") & "K"

This will allow more than one character at a time.

Scott Craner
  • 148,073
  • 10
  • 49
  • 81
  • Nice one. Could've thought about it myself, but i didn't :) I knew that Len of the string becomes 2 after using StrConv on one letter. Thanks! – ZygD Oct 14 '15 at 21:19
  • @ZygD thanks, glad I could help. I did a little more research and found the actual problem. See the edit. – Scott Craner Oct 14 '15 at 21:21