1
votes

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

1
Well, to begin with you cannot use .NET namespaces in your VBA code. VBA knows nothing about .NET, so what works in VB.NET isn't going to work in VBA unless the capability was in VBA already, some ten years ago. So there's no "Imports" (only Tools/References) and no UTF32Encoding. What happens if you try recording a macro on the manual search that does work? Do you get syntax that works when you run the resulting macro?Cindy Meister
Thanks for the tip! That helps me understand VBA/VB better. Also, I am new to macros, so I was unaware that you could record macros and that would turn into code. Thanks again! When I do that, it expresses the symbol bread as a composite of two UTF-16 values: ChrW(55356) & ChrW(57182) I could have looked that up here: fileformat.info/info/unicode/char/1f35e/index.htm The next question I have is if there is any intuitive link between the UTF-32 designation and the dual UTF-16 designation, or if I will have to re-key each one of my symbols in my source table.Jacob E
I have no idea about your second question, encoding isn't my "thing" :-) Best to ask that in a new Question so that you'll get better visibility. BTW since you're new on SO: it's considered good practice to "up-vote" things that help you. And it would be advisable for you to summarize the content and write it to the "Answer", then mark it as the answer so that others with the same question later can find it and know what worked.Cindy Meister

1 Answers

0
votes

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.