Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting character number in Word macro

Status
Not open for further replies.

KenWK

Programmer
May 25, 2001
76
US
Hey guys, I've been writing code in VBA for quite a while but have NEVER been able to find a way to get the number of a character inserted with Word's "Insert Symbol" menu while running a VBA macro. Asc, AscW and AscB always seem to return 40 for the character number.

Does anyone know of a way to get that number, and/or to search for a character inserted with the "Insert Symbol" menu?

Many Thanks
 
Hi Ken,

Don't really understand the question - get the character code when and where?

As for searching, characters are just characters after they've been inserted - they have no memory of having been inserted via the Symbol dialog so, again, I'm not really sure what you want to know.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Take a look at the Asc function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Actually PH the Asc function (as KenWK did in fact state) does NOT give the character number. I don't know a way to get it out.

The characternumber is set based on the sum of 31 and the location of the symbol on the font character set. You can have NEGATIVE numbers as characternumbers.

For example:

Code:
' insert a bizarre little number

Selection.InsertSymbol Font:="Verdana", _
characternumber:=1044, Unicode:= True

' select it
Selection.MoveRight Unit:=wdCharacter, Count:=-1, Extend:=wdExtend

Msgbox Asc(Selection.Text) & "  " & Selection.Text

will display "63" and "?"

Note that if you make the characternumber = -1044 you will get the same result. Make it 856, or - 415, you will still get the Asc function to = 63, and the "text" (as in Selection.Text) regardless of the symbol will be displayed as "?".

What Ken is, apparently, asking for is a way to get the font characternumber OUT. You can put one IN, by code, but as Tony pointed out, once IN - they are characters. Ken, I have searched all through the object model and there may be a way, but if there is, it is darn obscure....even for Microsoft.

Actually, Tony has a point. Why do you want to even know???? It seems a trifle...hmmmm...useless?





Gerry
 
My largest client has an author who created his 800+ page math-laden manuscript in Word and they want to send it to a compositor who uses LaTex to typeset. Well the compositor is saving the files as ASCII Text and not getting anything useful for all the special characters inserted with the Insert Symbol menu, as one would expect. Not a quick or easy task to look through 800 pages of manuscript to find all the silly little symbols that would need to be put back in.

They wanted me to give him files that wouldn’t take him as long to clean up. After coding the italic and other attributes with what he needed as well as searching and replacing the characters that were simply in the Symbol font, I ended up taking kind of a reverse approach. I wrote a massive search and replace routine that replaced all the regular characters to hidden, after which, only the characters inserted with the Insert Symbol menu were left. I was at least then able to give him a unique code he could search on to find what was missing. Not a 100% but much better than where he was.

This is something I wouldn’t mind having in the future as we do a lot of conversions so I’m going to keep checking on it from time to time.

Thanks for all your suggestions!
 
Ken,

What is your problem with AscW?

Gerry,

If you replace Asc with AscW in your code you get 1044. The actual character still displays as "?" but I suspect this is, in part, a limitation of the Msgbox function.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
I think it depends on the font that was selected in the Insert Symbol menu as to whether or not you get a unique value.

If you use "(normal text)" you typically get a unique number. If you select "Symbol," however, Asc, AscB and AscW all return 40 as the value.
 
And the compositor can't deal with the .doc file ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PH has an excellent point. If I understand it correctly, the original file IS a .doc. It is sent to the compositor who saves it as ASCII. OK, what exactly is the problem? Why does it have to be saved as ASCII?

This may be a bloody awful solution, and I may be going out on a limb considering who is posting in here....

Code:
Selection.InsertSymbol Font:="Verdana", CharacterNumber:=1044, Unicode:= _
        True
Selection.MoveRight Unit:=wdCharacter, Count:=-1, Extend:=wdExtend
MsgBox Selection.Font.NameAscii & " " & AscW(Selection.Text)
Selection.Collapse direction:=wdCollapseStart

will display both the font used for the symbol, AND the characternumber used.

Maybe you could use this information to:

Insert a Hidden Bookmark (with the _ character at the start of the bookmark name), and the rest of the name the CharacterNumber.

Use .Exists to check for existing bookmarks with that number, and if so, do an incremented appended counter so you get a unique identifying name.

Buld the document with hidden bookmarks that mark the location and type of symbol. Merge the incoming ASCII file with the original?

Of course the returning ASCII file may have (will have) changes. Build another document with an array of the fonts and characternumbers and use them to rebuild the symbols? Of course if they are not in the same order....

Just some thoughts.

Gerry
 
Hi Gerry,

I'm finding this fascinating and don't have any answers but if you change your code so that it inserts a character from the Symbol character set (with a negative characternumber) you do get rather different results. As Ken said in his original post, this always shows up as character 40 (left parenthesis) and also shows up as the underlying font - you can even see this in Word without going into VBA - Symbol is just not being treated like a normal font at all. I am investigating but getting nowhere!!

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Yes, this has piqued my interest as well, although, frankly, not for any real practical reasons.

It depends on the Font. "normal.text" font seems to work out fine, as does Symbol itself, but it is when you get into others that things appear to be wonky. I have now inserted, by code, a whole plethora of numbers, including large negative ones. Characternumber (if you look in Help on InsertSymbol) is a Long. Try putting in a characternumber of very large number. Like, oh I don't know, 31,077, or -31,077 (or -31 077 for those of you who prefer that notation). I did not know there were that many viable characters....but you do not get an error. Huh? It mentions a relative position, but what is -31,077 relative TO?

Also, for fun, try turning Unicode to False.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top