The following two macros convert numbers in the
selected range between Western and Arabic/Persian. The code also provides for numbers written left-to-right and for right-to-left - the supplied function does the reversing. Comments in the code show how to change the text direction and which source/target scripts to use.
For PC macro installation & usage instructions, see:
Installing Macros
For Mac macro installation & usage instructions, see:
Word:mac - Install a Macro
Code:
Sub WesternNumberToArabic_or_Persian()
Dim Rng As Range, StrTmp As String, i As Long
Set Rng = Selection.Range
With Selection.Range
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Text = "[,.0-9]{1,}"
.Replacement.Text = ""
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Sub
If .Characters.Last Like "[.,]" Then .End = .End - 1
' If the numbers are input right-to-left, use:
StrTmp = Reverse(.Text)
' If the numbers are input left-to-right, use:
StrTmp = .Text
For i = 0 To 9
' For arabic #s, use
StrTmp = Replace(StrTmp, Chr(48 + i), ChrW(17632 + i))
' For persian #s, use
StrTmp = Replace(StrTmp, Chr(48 + i), ChrW(1776 + i))
Next i
.Text = StrTmp
.Collapse (wdCollapseEnd)
Loop
End With
End Sub
Code:
Sub Arabic_or_PersianNumberToWestern()
Dim Rng As Range, StrTmp As String, i As Long
Set Rng = Selection.Range
With Selection.Range
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
'For arabic #s, use:
.Text = "[,." & ChrW(1632) & "-" & ChrW(1641) & "]{1,}"
'For persian #s, use
.Text = "[,." & ChrW(1776) & "-" & ChrW(1785) & "]{1,}"
.Replacement.Text = ""
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Sub
If .Characters.Last Like "[.,]" Then .End = .End - 1
' If the numbers are input right-to-left, use:
StrTmp = Reverse(.Text)
' If the numbers are input left-to-right, use:
StrTmp = .Text
For i = 0 To 9
' For arabic #s, use 1632
StrTmp = Replace(StrTmp, ChrW(1632 + i), Chr(48 + i))
' For persian #s, use 1776
StrTmp = Replace(StrTmp, ChrW(1776 + i), Chr(48 + i))
Next i
.Text = StrTmp
.Collapse (wdCollapseEnd)
Loop
End With
End Sub
Code:
Function Reverse(StrTmp As String) As String
If (Len(StrTmp) > 1) Then
Reverse = Reverse(Mid$(StrTmp, 2)) + Left$(StrTmp, 1)
Else
Reverse = StrTmp
End If
End Function