PDA

View Full Version : VBA FOR converting ascii to decimal codes in word 2003



Lio
11-26-2011, 02:58 AM
I am looking a word 2003 macro so that ASCII characters (such as àéæćźş) gets converted into there corresponding decimal codes like for é it is é etc, else it takes a lot time to find and replace one by one.

Kindly Suggest

Talis
11-26-2011, 12:03 PM
Sub AsciiToDec()
MsgBox "The Decimal Value is: " & Asc(Selection)
End Sub

Select the character then run the macro.

Lio
11-26-2011, 07:10 PM
thanks but what is required here is automatic find and replace all ASCII to corresponding Decimal code, as find and replacing 1 by 1 wont help if we have more then 300+,

looking forward

Talis
11-27-2011, 11:17 AM
Could you post a small sample of the text before it is converted and then the same text as how you would like it to appear?
I'm guessing you want to convert only those characters with an ASCII code greater than 191(decimal); however, in your original post you say "like for é it is é etc" which implies replace é with é which doesn't make sense.

Lio
11-27-2011, 12:01 PM
I posted it correctly but it got converted to symbol,

-------------Input----------
This is an example where in a para these letters comes in words è ì à ŏ œ æ ż Ź
-------------output------
Here the output should come as decimal codes for the corresponding character(special characters) with &# and the number like &# 233; (given a space here else it will show you symbol in your window)


So the main thing here is to design a macro in word 2003, where

macro searches ASCII for decimal codes between say 200-500 and automatically replaces with there corresponding decimal with starting as &# and after number ; &#...;

Talis
11-28-2011, 03:47 PM
Sub AsciiToDec()
Dim oRng As Word.Range
Dim char As Variant
Dim decmal As Long
Set oRng = ActiveDocument.Range
For Each char In oRng.Characters
If Asc(char) > 191 Then
decmal = Asc(char)
char.Select
Selection.Delete
Selection.InsertBefore Text:="&#" & decmal & ";"
End If
Next
End Sub

Lio
11-29-2011, 06:08 AM
Thanks, here I think we can increase or decrease the ASCII numbers and it may do the work,
let me try
-------------

Talis
11-29-2011, 10:20 AM
Sub AsciiToDec()
Dim oRng As Word.Range
Dim char As Variant
Dim decmal As Long

Const MinASC As Long = 191, MaxASC As Long = 256

Set oRng = ActiveDocument.Range
For Each char In oRng.Characters
If Asc(char) > MinASC And Asc(char) < MaxASC Then
decmal = Asc(char)
char.Select
With Selection
.Delete
.InsertBefore Text:="&#" & decmal & ";"
End With
End If
Next
End Sub

Change the values of the Constants to what you want.