PDA

View Full Version : Inserting Unicode character into VBA code



skeptic13
01-22-2013, 10:05 AM
I am writing a macro to reformat code for R (the statistical programming language) copied from a PDF document into a DOC file for copy/pasting into the R GUI.

There are a number of minor reformats required to be done in Word to produce code that the R GUI will implement.

One commonly used symbol is the (regular) tilde (U+007E). Unfortunately, the PDF has rendered this as either a small tilde (U+02DC) or a combining tilde (U+0303). (I can't tell yet which!)

I want my macro to make the necessary substitution.

This is as far as I've got - the code is as produced by the macro recorder, except for the .Text line.


Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ChrW$(732)
.Replacement.Text = "~"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

From searching online, I believe that it is possible to make references to Unicode characters by code number using ChrW(x), where 'x' is the decimal equivalent of the Unicode hex code.

Thus, 0732 (leading zero is, I assume, meaningless) is the decimal equivalent of the hex for the small tilde. (I have tried 0771 - hex for combining tilde too - both with and without the dollar sign!)

I should say that the problem arises because the Courier font in the code window does not render the tilde copied from the PDF at all. While recording the macro, I copy/paste the tilde directly from the PDF into the Find/Replace dialog box (where it shows up as expected). But when I come to look at the code produced by the recorder in the VBE, where the tilde should be just shows "".

All help gratefully accepted!

macropod
01-29-2013, 12:44 AM
What do you get if you select the offending character and run:
MsgBox AscW(Selection.Text)