Skip to content

Instantly share code, notes, and snippets.

@touchiep
Last active February 18, 2024 09:16
Show Gist options
  • Save touchiep/f95e672fd45c22a0e6970ee25b92e7b2 to your computer and use it in GitHub Desktop.
Save touchiep/f95e672fd45c22a0e6970ee25b92e7b2 to your computer and use it in GitHub Desktop.
[VBA][Excel] Convert text to phonetics alphabet. Support English and Thai only.
Function Phonetics(InputWord As String, Optional EngType As String = "ICAO") As String
'Convert Words to Phonetic Alphabet
'Phonetics version 1.0 by Pongsathorn Sraouthai
'Support English and Thai only
'EngType:
'ICAO = ICAO phonetics alphabet
'animal = animal phonetics alphabet
'fruit = fruit phonetics alphabet
'baby = baby phonetics alphabet
'police = Law Enforcement phonetics alphabet
Dim T1 As Variant
Dim R1 As Variant
Dim TArr As Variant
Dim RArr As Variant
Dim i, ss, rr, tt, Ut, Ls, ICAO, animal, fruit, baby, police
Phonetics = ""
Ut = UCase(InputWord)
For i = 1 To Len(Ut)
Ls = AscW(Mid(Ut, i, 1))
Select Case Ls
Case 48 To 57, 65 To 90
LangID = "en"
Case Else
LangID = "th"
End Select
Select Case LangID
Case "EN", "en"
ICAO = "Alfa-Bravo-Charlie-Delta-Echo-Foxtrot-Golf-Hotel-India-Juliett-Kilo-Lima-Mike-November-Oscar-Papa-Quebec-Romeo-Sierra-Tango-Uniform-Victor-Whisky-Xray-Yankee-Zulu-Zero-Wun-Too-Tree-Fower-Fife-Siks-Seven-Ate-Niner- "
animal = "Ant-Bat-Cat-Dog-Eagle-Fox-Goat-Horse-Icefish-Jaguar-Kangaroo-Lion-Monkey-Newt-Owl-Parrot-Quail-Rabbit-Snail-Tiger-Unicorn-Viper-Whale-Xavier-Yak-Zebra-Zero-One-Two-Three-Four-Five-Six-Seven-Eight-Nine- "
fruit = "Apple-Banana-Coconut-Durian-Eggplant-Fig-Grape-Honeydew-Ice apple-Jackfruit-Kiwi-Lemon-Mango-Nut-Orange-Plum-Quince-Raspberry-Strawberry-Tomato-Ugli-Vanilla-Watermelon-Xigua-Yuzu-Zucchini-Zero-One-Two-Three-Four-Five-Six-Seven-Eight-Nine- "
baby = "Apple-Banana-Cat-Dog-Elephant-Frog-Goat-Hamburger-Igloo-Jellyfish-Kiwi-Lion-Monkey-Nest-Orange-Panda-Queen-Rabbit-Strawberry-Tiger-Umbrella-Violin-Watermelon-Xylophone-Yak-Zebra-Zero-One-Two-Three-Four-Five-Six-Seven-Eight-Nine- "
police = "Adam-Boy-Charles-David-Edward-Frank-George-Henry-Ida-John-King-Lincoln-Mary-Nora-Ocean-Paul-Queen-Robert-Sam-Tom-Union-Victor-William-Xray-Young-Zebra-Zero-One-Two-Three-Four-Five-Six-Seven-Eight-Nine- "
T1 = "A~B~C~D~E~F~G~H~I~J~K~L~M~N~O~P~Q~R~S~T~U~V~W~X~Y~Z~0~1~2~3~4~5~6~7~8~9~ "
Select Case EngType
Case "ICAO": R1 = ICAO
Case "animal": R1 = animal
Case "fruit": R1 = fruit
Case "baby": R1 = baby
Case "police": R1 = police
End Select
Case "TH", "th"
T1 = "ก~ข~ฃ~ค~ฅ~ฆ~ง~จ~ฉ~ช~ซ~ฌ~ญ~ฎ~ฏ~ฐ~ฑ~ฒ~ณ~ด~ต~ถ~ท~ธ~น~บ~ป~ผ~ฝ~พ~ฟ~ภ~ม~ย~ร~ล~ว~ศ~ษ~ส~ห~ฬ~อ~ฮ~ะ~า~ิ~ี~ึ~ื~ุ~ู~เ~แ~โ~ใ~ไ~ั~็~ำ~ฤ~ฦ~ๅ~่~้~๊~๋~์~ๆ~ฯ~.~,~?~'~!~/~(~)~*~:~;~=~-~_~""~ํ~ฺ~๐~๑~๒~๓~๔~๕~๖~๗~๘~๙~ "
R1 = "ก ไก่-ข ไข่-ฃ ฃวด-ค ควาย-ฅ ฅน-ฆ ระฆัง-ง งู-จ จาน-ฉ ฉิ่ง-ช ช้าง-ซ โซ่-ฌ เฌอ-ญ หญิง-ฎ ชฎา-ฏ ปฏัก-ฐ ฐาน-ฑ มณโฑ-ฒ ผู้เฒ่า-ณ เณร-ด เด็ก-ต เต่า-ถ ถุง-ท ทหาร-ธ ธง-น หนู-บ ใบไม้-ป ปลา-ผ ผึ้ง-ฝ ฝา-พ พาน" & _
"-ฟ ฟัน-ภ สำเภา-ม ม้า-ย ยักษ์-ร เรือ-ล ลิง-ว แหวน-ศ ศาลา-ษ ฤๅษี-ส เสือ-ห หีบ-ฬ จุฬา-อ อ่าง-ฮ นกฮูก-สระอะ-สระอา-สระอิ-สระอี-สระอึ-สระอื-สระอุ-สระอู-สระเอ-สระแอ-สระโอ-ไม้ม้วน-ไม้มลาย-ไม้หันอากาศ" & _
"-ไม้ไต่คู้-สระอำ-ตัวรึ-ตัวลึ-ลากข้าง-ไม้เอก-ไม้โท-ไม้ตรี-ไม้จัดวา-ทัณฑฆาต-ไม้ยมก-ไปยาลน้อย-มหัพภาค-จุลภาค-ปรัศนี-ฝนทอง-อัศเจรีย์-ทับ-วงเล็บเปิด-วงเล็บปิด-ดอกจัน-ทวิภาค-อัฒภาค-เสมอภาค-ยัติภังค์-สัญประกาศ-ฟันหนู-นิคหิต-พินทุ" & _
"-ศูนย์-หนึ่ง-สอง-สาม-สี่-ห้า-หก-เจ็ด-แปด-เก้า- "
End Select
TArr = Split(T1, "~")
RArr = Split(R1, "-")
tt = Mid(Ut, i, 1)
rr = IsInArray(CStr(tt), TArr)
ss = Replace(tt, TArr(rr), RArr(rr))
Phonetics = Phonetics & ss & " "
Next i
End Function
Private Function IsInArray(stringToBeFound As String, arr As Variant) As Long
Dim i As Long
' default return value if value not found in array
IsInArray = -1
For i = LBound(arr) To UBound(arr)
If StrComp(stringToBeFound, arr(i), vbBinaryCompare) = 0 Then
IsInArray = i
Exit For
End If
Next i
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment