PDA

View Full Version : Help Needed with macro to identify symbols within a given string



lamb0878
06-05-2012, 11:12 AM
Hi All,

A long time watcher of this forum and have learned so much from you all, but now I am faced with a question/problem of my own.

Im trying to create macro (word 2007) that will identify if a selected range contains any symbols that are not part of a predetermined list of "compliant symbols".

Right now, when a user selects a range and the macro is called, it looks at the asc character of each character in the range and checks the asc# against a list of "compliant" asc numbers...

The major problem is that the left parenthesis is being returned as asc 40...which the same asc # as many of the the "non compliant" symbols I am trying to find.

Is there any ay to find symbols within a string by asc #? Keep in mind, I will not know which symbols to look for..I researched the macros9.dot and it doesnt seem to help..

Thanks for your help.:dunno


When I try MsgBox AscW(.Paragraphs(1).Range.Characters(1)) it returns -3842 regardless of whether its left parens or a Beta symbol.

Frosty
06-05-2012, 11:40 AM
I don't get that result with AscW... left paren is 40, and beta is 946. However, perhaps you just need to apply a bit of a filter...


Dim sChar As String
sChar = .Paragraphs(1).Range.Characters(1)
If sChar <> "(" Then
Msgbox AscW(sChar)
End if

lamb0878
06-05-2012, 11:53 AM
I don't get that result with AscW... left paren is 40, and beta is 946. However, perhaps you just need to apply a bit of a filter...


Dim sChar As String
sChar = .Paragraphs(1).Range.Characters(1)
If sChar <> "(" Then
Msgbox AscW(sChar)
End if


Thanks but this isnt helping me because the left parens is indeed being returned as 40 for me.. so if i filter it out, i end up filtering out the real symbols i want to catch which for some reason are being returned as 40.

Is this a known issue?

Frosty
06-05-2012, 11:56 AM
Did you try that code?
Otherwise, you will need to post a sample doc with the text in it. If the codes are being turned into actual left parens by some other process, then you will need to give a more full explanation.

Yes, some text will turn into other text depending on the scenario... But that means you'll have to perform your test before this transformation.

lamb0878
06-05-2012, 12:09 PM
Did you try that code?
Otherwise, you will need to post a sample doc with the text in it. If the codes are being turned into actual left parens by some other process, then you will need to give a more full explanation.

Yes, some text will turn into other text depending on the scenario... But that means you'll have to perform your test before this transformation.

yes i tried the code..no other process is invoked. i get message box confirming char 40 for left parens as well as beta symbol in the string im working with,

Any ideas?

lamb0878
06-05-2012, 12:16 PM
Attached is sample file with the string in question.

Frosty
06-05-2012, 05:04 PM
That's because one of the characters has the font Arial, and the other one has the font Symbol.

So you need to test for the Symbol font. I don't know if this will help throughout, since you may have actual different symbols, and you may have different fonts to give you what you see in the document.

But this may help

Public Sub Demo()
Dim rngChar As Range

For Each rngChar In Selection.Range.Characters
If rngChar.Font.Name = "Symbol" Then
MsgBox "This is formatted as a symbol " & vbCr & _
"and has the AscW char code: " & AscW(rngChar.text)
End If
Next
End Sub

Frosty
06-05-2012, 05:11 PM
I'm not sure by what process the left paren character is inserted into your document and then formatted as the Symbol font, but there is a distinct difference between font formatting and the actual Asc character used. Depending on your documents, your AscW approach may be completely invalid. You may need to get a list of the characters you have in use which are formatted with the Symbol font, and come up with your own translator, rather than the AscW/Asc functions.

lamb0878
06-05-2012, 06:32 PM
I appreciate your time and responses.. I am still getting 40 on both the parens and the beta symbol.

I did make some headway tho, that i thought i would share...it seems that symbols (once inserted into the document) can become "locked"..this is what i learned from another forumm..once hey are "locked" (this is a ms word quirk", they are set as asc 40..(regardless of what they are)...

in order to discern what symbol is truly what, you must first run a routine that "unlocks them" and reveals their true ascw number.

Frosty
06-05-2012, 06:57 PM
Well, if that's true, that's not what was in your sample document. You simply had two left parens right next to each other. The first was formatted in Arial font, so that it looks like its true character (a left paren), and the second left paren was formatted in Symbol font, which caused it to look like a Beta symbol. The same thing would happen with any of the fonts in that class (wingdings, etc).

If you can give a more comprehensive sample, you might get farther here... But I suspect you need to understand a little better how your "symbols" are being inserted... Because there is nothing mysterious about what you've posted... You simply have the wrong approach to get the result you want

Frosty
06-05-2012, 07:00 PM
Oh, and if you have cross-posted this elsewhere, you should really link to it, so that people will know it has been asked (and perhaps answered) elsewhere before spending time on a question you've already had answered elsewhere by the time they read your post here