Sub Universal_Converter_v1.0()
'
' Universal_Converter
'
' Proprietary fonts to Unicode font Converter Macro for MS Word
' First Release: Version 1.0, 24 December 2015.
'
' Only selected font is changed to Unicode.
' Hence, ensure that the font of the text you intend to convert, has been set to
that font.
' If there are more than one font that you intend to convert, please run the macro
many times, each time selecting one font for conversion
'All Unicode codes of letters that combine with other letters into one single
entity are stored for matching and keeping letters separated.
Dim arrMaatraasCode
arrMaatraasCode = Array(2366, 2367, 2368, 2369, 2370, 2371, 2372, 2375, 2376,
2377, 2379, 2380, 2306, 2373, 2305, 2307, 2381, 2364)
'aa i ee u oo ri rri e ai au o ou bindu, e, chandrabindu, visarg, halant, nuqta
'This converts above codes to letters and puts them in a single variable
Dim arrMaatraas(17) As String
strMaatraasAll = " "
For arrIndex = 0 To UBound(arrMaatraasCode)
arrMaatraas(arrIndex) = ChrW(arrMaatraasCode(arrIndex))
strMaatraasAll = strMaatraasAll & arrMaatraas(arrIndex) & " "
Next arrIndex
'to prevent matraas and samyuktakshars from merging, yet not give a word break
'a of sinhala, dec code 3845
strFiller = ChrW(3845)
strHalant = ChrW(2381)
strChhoti_i = ChrW(2367)
' bindu, anuswara, dec code 2306, also to be moved forward if immediately with
chhoti i
strBindu = ChrW(2306)
strR = ChrW(2352)
'0: No, 1: yes, Change accordingly if you want to create the output conversion
table or not, helps in debugging
CreateCSVTable = 1
'subroutine reads conversion table from disk file and optionally creates only
applicable values to another CSV disk file
ReadConversionTable arrInCode, arrFromChar, arrToChar, arrIndexMax,
strMaatraasAll, strFiller, CreateCSVTable, oFontFrom, oFontTo
With Selection.Find.Font
.Name = oFontFrom
'.Size = 10
'.Bold = False
'.Italic = True
'.Color = wdColorRed
End With
Application.ScreenUpdating = True
'Application.ScreenUpdating = False 'stops word file displaying changes. If you
like to see each letter getting converted, make it "true", or comment it
For arrIndex = 0 To arrIndexMax
If (arrInCode(arrIndex) >= 1) And (arrInCode(arrIndex) <= 4) Then
ReplaceText1 arrFromChar(arrIndex), arrToChar(arrIndex)
End If
Next arrIndex
rngStory.Find.Execute Replace:=wdReplaceAll
Next rngStory
oFontFrom1 = oFontFrom
Select Case oFontFrom
Case "Ankit"
'Ankit's charmap is exactly same as Kruti Dev 010
oFontFrom1 = "Kruti Dev 010"
Case "Kruti Dev 016"
'Kruti Dev 016's charmap is almost similar to that of Kruti Dev 010
oFontFrom1 = "Kruti Dev 010"
End Select
'change to reflect the location of conversion table files in your hard disk
oPath = "E:\lang\fr\"
oInFontFile = oPath & oFontFrom1 & "_to_" & oFontTo & ".txt"
'open conversion table file as read only
Const flagReadOnly = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileIn = objFSO.OpenTextFile(oInFontFile, flagReadOnly)
arrIndex = -1
Do Until objFileIn.AtEndOfStream
strLine = objFileIn.ReadLine
'the input read from the file is split by TABs into different fields
arrFields = Split(strLine, vbTab)
If CreateCSVTable = 1 Then
objFileOut.WriteLine arrIndex & "," & arrInCode(arrIndex) & "," &
Chr(34) & arrFromChar(arrIndex) & Chr(34) & "," & Chr(34) & arrToChar(arrIndex) &
Chr(34)
End If
ContinueUntilLoop1:
Loop
'Now, this has maximum value of index up to which there are valid values in the
conversion array
arrIndexMax = arrIndex
objFileIn.Close
If CreateCSVTable = 1 Then
objFileOut.Close
End If
End Sub 'ReadConversionTable
Function InsertFiller(ByVal inStr1 As String, ByVal strMaatraasAll As String, ByVal
strFiller As String) As String
'
' InsertFiller
'
' adds strFiller to keep letters separate
InsertFiller = oStr2
End Function 'InsertFiller
Function UTF8_Decode(ByVal sStr As String)
'
' UTF8_Decode
'
' taken from http://p2p.wrox.com/vbscript/29099-unicode-utf-8-system-text-
utf8encoding-vba.html
' converts corrupted unicode text to utf-8 which is otherwise stored as ANSI-2354-4
etc.
'
Dim l As Long, sUTF8 As String, iChar As Integer, iChar2 As Integer
For l = 1 To Len(sStr)
iChar = Asc(Mid(sStr, l, 1))
If iChar > 127 Then
If Not iChar And 32 Then ' 2 chars
iChar2 = Asc(Mid(sStr, l + 1, 1))
sUTF8 = sUTF8 & ChrW$(((31 And iChar) * 64 + (63 And iChar2)))
l = l + 1
Else
Dim iChar3 As Integer
iChar2 = Asc(Mid(sStr, l + 1, 1))
iChar3 = Asc(Mid(sStr, l + 2, 1))
sUTF8 = sUTF8 & ChrW$(((iChar And 15) * 16 * 256) + ((iChar2 And 63) *
64) + (iChar3 And 63))
l = l + 2
End If
Else
sUTF8 = sUTF8 & Chr$(iChar)
End If
Next l
UTF8_Decode = sUTF8
End Function 'UTF8_Decode
Sub ReplaceText1(ByVal oText As String, ByVal oReplacementText As String)
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = oText
.Replacement.Text = oReplacementText
Selection.Find.Execute Replace:=wdReplaceAll
End Sub 'ReplaceText1
Sub Shift_chhotii_i(ByVal strFiller As String, ByVal strHalant As String, ByVal
strChhoti_i As String, ByVal strBindu As String)
'chhoti i to be moved forwards, dec code 2367
lenFiller = Len(strFiller)
lenHalant = Len(strHalant)
strFillerHalant = strFiller & strHalant
lenFillerHalant = Len(strFillerHalant)
strSearchChhoti_i1 = strFiller & strChhoti_i
lenSearchChhoti_i1 = Len(strSearchChhoti_i1)
strSearchBindu1 = strFiller & strBindu
lenSearchBindu1 = Len(strSearchBindu1)
Selection.HomeKey Unit:=wdStory
Dim oRange As Range
Set oRange = ActiveDocument.Range
With Selection.Find
.ClearFormatting
.Text = strSearchChhoti_i1
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
Selection.MoveEnd Unit:=wdWord, Count:=1
strMemText = Selection.Range.Text
lenSelection = Len(strMemText)
lenMid = 0
Do While (Mid(strMemText, lenSearched1 + lenMid + 3, lenHalant) =
strHalant)
lenMid = lenMid + 1 + lenFillerHalant
Loop
Selection.Range.Text = Mid(strMemText, lenSearched1 + 1, lenMid + 1) &
_
Left(strMemText, lenSearched1) & _
Right(strMemText, lenSelection - lenSearched1 -
lenMid - 1)
Selection.MoveStart Unit:=wdCharacter, Count:=lenMid + lenSearched1
Loop
End With
End Sub 'Shift_chhotii_i
Sub Shift_half_r(ByVal strFiller As String, ByVal strHalant As String, ByVal strR
As String)
'half r to be moved backwards, dec code 2352, followed by halant
lenFiller = Len(strFiller)
lenHalant = Len(strHalant)
strFillerHalant = strFiller & strHalant
lenFillerHalant = Len(strFillerHalant)
strSearched = strR & strFillerHalant
lenSearched = Len(strSearched)
Selection.HomeKey Unit:=wdStory
Dim oRange As Range
Set oRange = ActiveDocument.Range
With Selection.Find
.ClearFormatting
.Text = strSearched
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
Selection.MoveStart Unit:=wdWord, Count:=-1
strMemText = Selection.Range.Text
lenSelection = Len(strMemText)
lenMid = 0