Inserting Unicode Characters

  • Thread starter Becky Carter Hickman-Jones
  • Start date
B

Becky Carter Hickman-Jones

Hi,

In one of the inherited macros I've got here at work, there is a list of
commonly-used but hard-to-locate mathematical and scientific characters
displayed in a menu for authors to get at easily rather than hunting for.
One of the macros, which simply inserts the desired character, reads, for
example:

Public Sub Alpha()
DoSymbolInsertion True, 945, True
End Sub

The "DoSymbolInsertion" macro reads thusly:

Private Sub DoSymbolInsertion(bDoSpaces As Boolean, lCharCode As Long,
bUseFontStyle As Boolean)
'If bDoSpaces Then
' Selection.InsertAfter " "
' Selection.Collapse wdCollapseEnd
' Selection.Move Unit:=wdCharacter, Count:=-1
'End If
If bUseFontStyle Then
Selection.Range.InsertSymbol Font:="Times New Roman",
CharacterNumber:=lCharCode, unicode:=True
Else
Selection.Range.InsertSymbol CharacterNumber:=lCharCode,
unicode:=True
End If
If bDoSpaces Then
Selection.Move wdCharacter, 1
End If
Exit Sub
End Sub

I have dozens of these little scripts to insert dozens of different
characters. They work fine, but I need to add new characters, and I'm not
sure how to represent them in this format. I mean, what is the number "945"
in the Alpha script above? It's not the unicode character number of the
lower case Greek alpha (in this example), and it's not the ASCII (decimal)
value either, or at least not as listed by Word in the Insert > Symbol box.
So I'm not sure where the number came from and what its equivalent is. The
person who created these macros has fallen off the face of the Earth, and I
can't get an answer from anyone else. In fact, I'm not even sure exactly
what the DoSymbolInsertion macro does! All I know is that it works and I
need to add to it.

Could I get some advice or a point in the right direction?

Thanks,
Beck
 
J

Joost Verdaasdonk

Hi Beck,

The easy way is to record the insertion of the symbol!

The sub above inserts the Little alpha letter. When you
record this sub with the recorder you get:

Sub Macro2()
Selection.InsertSymbol Font:="Times New Roman",
CharacterNumber:=945, _
Unicode:=True
End Sub

So its easy to retrieve the symbol number from the
recorder and thus easy to extend you're symbol macro's.

In the VBE select insertSymbol en press F1 to gain more
information about how CharacterNumber gets build.

Enjoy,
Joost Verdaasdonk
 
K

Klaus Linke

[...] I'm not sure how to represent them in this format.
I mean, what is the number "945" in the Alpha script above?
It's not the unicode character number of the lower case Greek
alpha (in this example), and it's not the ASCII (decimal) value
either, or at least not as listed by Word in the Insert > Symbol box.


Hi Becky,

It *is* the Unicode character code, only in the (uncommon) decimal notation
rather than the hexadecimal notation (which is used in "Insert > Symbol"
and elsewhere).

Decimal 945 = Hexadecimal 3B1
(or 9*10^2 + 4*10^1 + 5*10^0 = 3*16^2 + B*16^1 + 1*16^0)

You can use &H3B1 instead of 945 anywhere, if you prefer the hexadecimal
notation:
DoSymbolInsertion True, &H3B1, True
The "&H" prefix tells VBA that the following stuff is a hex number.

If you want to change your already existing macros to hexadecimal notation,
you might make use of the immediate window as a conversion calculator:
? Hex(945)
3B1
? &H3B1
945

Regards,
Klaus
 
B

Becky Carter Hickman-Jones

Klaus Linke said:
[...] I'm not sure how to represent them in this format.
I mean, what is the number "945" in the Alpha script above?
It's not the unicode character number of the lower case Greek
alpha (in this example), and it's not the ASCII (decimal) value
either, or at least not as listed by Word in the Insert > Symbol box.


Hi Becky,

It *is* the Unicode character code, only in the (uncommon) decimal notation
rather than the hexadecimal notation (which is used in "Insert > Symbol"
and elsewhere).

Decimal 945 = Hexadecimal 3B1
(or 9*10^2 + 4*10^1 + 5*10^0 = 3*16^2 + B*16^1 + 1*16^0)

You can use &H3B1 instead of 945 anywhere, if you prefer the hexadecimal
notation:
DoSymbolInsertion True, &H3B1, True
The "&H" prefix tells VBA that the following stuff is a hex number.

If you want to change your already existing macros to hexadecimal notation,
you might make use of the immediate window as a conversion calculator:
? Hex(945)
3B1
? &H3B1
945

Regards,
Klaus

Thank you, Klaus. That helps me immensely! I will definitely use this in
updating the macros.

Beck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top