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