Printing UTF32 Symbols in Word Macros
Question: I am playing around with VBA for Word, and I am working on a project to do a
batch find/replace in a word document. I imported a list of find/replace terms
from a .csv.

The issue I'm running into is that I want to replace words with symbols that
represent the words. For example, if I use the word bread, I want to replace
it with the UTF-32 symbol for bread (Unicode decimal 127838/ hex 0x1f35e).
When I was doing this manually using the built in MS-Word find/replace, this
worked fine, I would simply type in alt+127838 in the replace box and it would
work without a hitch, but doing UTF-32 find/replace in batch seems to be
giving me trouble.

If I use ChrW() it will be out of range because ChrW only accepts values up to
65,535. It can't handle the 17th bit. It gives me a runtime error if I try
plugging that in.

I tried using a \U escape character, but then it only replaced it literally
with "\U127838" which isn't very helpful. Not sure if VBA even supports the
unicode escape character. If I don't put it in quotes, it gives me a syntax
error.

Although I am not new to programming, I am new to VBA and VB. I checked
online, and it seems the UTF32Encoding class for VB doesn't work with VBA

<https://msdn.microsoft.com/en-
us/library/system.text.utf32encoding(v=vs.90).aspx>

It could be that I'm unfamiliar with the nuances between VB and VBA, but when
I tried the initializer:

Dim u32LE As New UTF32Encoding(False, True)

It gave me a syntax error in my VBA code.

I tried using the Imports statement, but that also confused my compiler. I'm
not sure if I'm doing something wrong, or if VBA doesn't support this class.

Let me know if VBA just doesn't support printing out UTF32 characters and I
should try using something like Python or Java instead. Your help is much
appreciated!

Here is a look at the function I'm writing. I commented out the Imports line
because it gave me an error (it says method or member not found highlighting
".Text")

    
    
    Sub findReplaceUnicode(ByVal findItem As String, unicode As Long)
    
    'Imports System.Text
    Dim u32LE As New UTF32Encoding(False, True)
    
    
    
    Selection.find.ClearFormatting
    Selection.find.Replacement.ClearFormatting
    With Selection.find
        .Text = findItem
        .Replacement.Text = ChrW(unicode)
        .Replacement.font.Name = "Segoe UI Symbol"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.find.Execute replace:=wdReplaceAll
    

End Sub


Answer: Well, to begin with you cannot use .NET namespaces in your VBA code. It is a
false lead to use a VB class in a VBA program. There is no compatibility.

The way to solve this problem is to not use UTF32. However, many of these
symbols also have a dual UTF-16 encoding. For example, the bread symbol
(UTF-32 0x1f35e) can also be typed by two UTF-16 entries: 0xD83C 0xDF5E
(d83cdf5e).

<http://www.fileformat.info/info/unicode/char/1f35e/index.htm>

File Format.info is a useful site in finding that translation. A way to
replace a symbol with over 17 bits would be to enter:

.Replacement.Text = ChrW(first) & ChrW(second)

Where first is the first half of the composite UTF-16 entry and second is the
second half of the composite UTF-16 entry.

A good rule of thumb is when in doubt, record a macro manually, then reverse
engineer the results.

