Using Asc and Chr

G

Greg Maxey

This is an ongoing issue in the Document Management group and I wanted
to bring it up here in hopes of getting the right people engaged.

If I insert any one of the ten "arrow" symbols using
Insert>Symbol>Symbols>Font Symbol and run the following code with any
one of theses symbols selected it returns a value of 40 and inserts a
left side paren symbol:

Sub Test()
Dim pAsc As Long
pAsc = Asc(Selection)
Selection.Range.InsertAfter Chr(pAsc)
End Sub

Why does this happen?

The question in the Documanagement group is how do you get the correct
ASCII code for symbols?

Thanks.
 
D

Dave Lett

Hi Greg,

Have a look at the article "Finding and replacing symbols" at
http://word.mvps.org/FAQs/MacrosVBA/FindReplaceSymbols.htm

Does the following help??

Dim sFont As String
Dim lCharNum As Long
Dim oRng As Range
Set oRng = Selection.Range
oRng.Collapse Direction:=wdCollapseEnd
With Dialogs(wdDialogInsertSymbol)
sFont = .Font
lCharNum = .CharNum
End With
oRng.InsertSymbol CharacterNumber:=lCharNum, Font:=sFont, Unicode:=True

HTH,
Dave
 
D

Dave Lett

Hi Greg,

I forgot to add probably the most important part: Why does this happen?

According to the Word VBA help topic "InsertSymbol Method":

Unicode Optional Variant. True to insert the unicode character specified
by CharacterNumber; False to insert the ANSI character specified by
CharacterNumber. The default value is False.

Therefore, it's happening because you're inserting the ANSI character,
instead of the Unicode character, I believe.

HTH,
Dave
 
G

Greg Maxey

Dave,

Thanks. The key quesition from the OP in the docmenent management is
"how do you determine the ASCII code of symbol?"

Using this adaptation of the code you provided with a right arrow
selected:

Sub Test1()
Dim sFont As String
Dim lCharNum As Long
Dim oRng As Range
Set oRng = Selection.Range
oRng.Collapse Direction:=wdCollapseEnd
With Dialogs(wdDialogInsertSymbol)
sFont = .Font
lCharNum = .CharNum
MsgBox lCharNum
End With
End Sub

I get -3922. The OP thinks the correct code is 117. I am not very
knowledgeable with ASCII codes so therefore I don't know if I am coming
closer to answering the question or not ;-)
 
H

Helmut Weber

Hi Greg,

add 4096 to the negative value, you get.
I think, the OP is wrong.

It is chr(174).

If you google for 4096 and my decent, ordinary name,
you'll find more information, some of which solved some problems.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Dave Lett

Hi Greg,

If a little knowledge is a dangerous thing, then I'm lethal, man, LETHAL.

ASCII and Unicode are different. ASCII limits you to 256 different
characters; Unicode limits you to 65,536 different characters. Therefore,
sometimes ASCII cannot support the symbol you really want to create. If you
want to see what ASCII character you will insert when using Chr(pAsc), then
have a look at the VBA help topic "Character Set (0 - 127)." So, the OP can
see that if he uses Chr(117), then VBA will insert a lowercase "u."

HTH,
Dave
 
G

Greg Maxey

Helmut,

I agree the OP must be mistaken, because as Dave says and I confirmed
122 produces a small case "u." I am not sure I fully understand the
relationship of 4096, the unicode, and the ASCII code. Using Chr(174)
I get the symbol for "registered" or a circle inscribing the letter R.


Perhaps the answer is that you can't determine the ASCII character of a
symbol that is not part of the ASCII set.
 
H

Helmut Weber

Hi Greg,
Perhaps the answer is that you can't determine the ASCII character of a
symbol that is not part of the ASCII set.

very much so.

The ultimate authority, IMHO, is Klaus Linke.
Whether decorative or unicode or farsi
or east asian or right to left hebrew or arabic,
if Klaus doesn't know, well, at least I don't.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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