Convert Currency in Number to Words in MS Word

Some times we need numbers in both numerical format and in words. Here I am going to explain how to use VBA module to convert Number to Words in Microsoft Word.

If you are looking for a method to use in MS - Excel, please check my earlier blog post : Convert Currency in Number to Words (Indian Rupees) - MS Excel 

 

Step 1: Enable Developer Tab In MS Word

If you can't see the developer tab as like in the below picture, Go to File -> Options -> Customize Ribbon -> Check on 'Devloper' under Main Tabs heading, Developer menu enabling process is explained here.

 

 

Step 2: Add the Module Code

Click on the Visual Basic option in the Developer Ribbon. Right click on the 'Project' title and Insert -> Module

 

Copy and paste the below code in code window of the Module1. 

  1. Sub ConvertCurrencyToEnglish()
  2. ' For more info, visit : www.livetolearn.in
  3. MyNumber = Val(Selection.Text)
  4.  
  5. Dim Temp
  6. Dim Rupees, Paise
  7. Dim DecimalPlace, Count
  8.  
  9. ReDim Place(9) As String
  10. Place(2) = " Thousand "
  11. Place(3) = " lakh "
  12. Place(4) = " Crore "
  13.  
  14.  
  15. ' Convert MyNumber to a string, trimming extra spaces.
  16. MyNumber = Trim(Str(MyNumber))
  17.  
  18. ' Find decimal place.
  19. DecimalPlace = InStr(MyNumber, ".")
  20.  
  21. ' If we find decimal place...
  22. If DecimalPlace > 0 Then
  23. ' Convert Paise
  24. Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
  25. ' Hi! Note the above line Mid function it gives right portion
  26. ' after the decimal point
  27. 'if only . and no numbers such as 789. accures, mid returns nothing
  28. ' to avoid error we added 00
  29. ' Left function gives only left portion of the string with specified places here 2
  30.  
  31.  
  32. Paise = ConvertTens(Temp)
  33.  
  34.  
  35. ' Strip off paise from remainder to convert.
  36. MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
  37. End If
  38.  
  39. Count = 1
  40. If MyNumber <> "" Then
  41.  
  42. ' Convert last 3 digits of MyNumber to Indian Rupees.
  43. Temp = ConvertHundreds(Right(MyNumber, 3))
  44.  
  45. If Temp <> "" Then Rupees = Temp & Place(Count) & Rupees
  46.  
  47. If Len(MyNumber) > 3 Then
  48. ' Remove last 3 converted digits from MyNumber.
  49. MyNumber = Left(MyNumber, Len(MyNumber) - 3)
  50. Else
  51. MyNumber = ""
  52. End If
  53.  
  54. End If
  55.  
  56. ' convert last two digits to of mynumber
  57. Count = 2
  58.  
  59. Do While MyNumber <> ""
  60. Temp = ConvertTens(Right("0" & MyNumber, 2))
  61.  
  62. If Temp <> "" Then Rupees = Temp & Place(Count) & Rupees
  63. If Len(MyNumber) > 2 Then
  64. ' Remove last 2 converted digits from MyNumber.
  65. MyNumber = Left(MyNumber, Len(MyNumber) - 2)
  66.  
  67. Else
  68. MyNumber = ""
  69. End If
  70. Count = Count + 1
  71.  
  72. Loop
  73.  
  74.  
  75.  
  76.  
  77. ' Clean up rupees.
  78. Select Case Rupees
  79. Case ""
  80. Rupees = ""
  81. Case "One"
  82. Rupees = "One Rupee"
  83. Case Else
  84. Rupees = Rupees & " Rupees"
  85. End Select
  86.  
  87. ' Clean up paise.
  88. Select Case Paise
  89. Case ""
  90. Paise = ""
  91. Case "One"
  92. Paise = "One Paise"
  93. Case Else
  94. Paise = Paise & " Paise"
  95. End Select
  96.  
  97. If Rupees = "" Then
  98. Result = Paise
  99. ElseIf Paise = "" Then
  100. Result = Rupees
  101. Else
  102. Result = Rupees & " and " & Paise
  103. End If
  104. Selection.Text = Result
  105.  
  106.  
  107. End Sub
  108.  
  109.  
  110. Private Function ConvertDigit(ByVal MyDigit)
  111. Select Case Val(MyDigit)
  112. Case 1: ConvertDigit = "One"
  113. Case 2: ConvertDigit = "Two"
  114. Case 3: ConvertDigit = "Three"
  115. Case 4: ConvertDigit = "Four"
  116. Case 5: ConvertDigit = "Five"
  117. Case 6: ConvertDigit = "Six"
  118. Case 7: ConvertDigit = "Seven"
  119. Case 8: ConvertDigit = "Eight"
  120. Case 9: ConvertDigit = "Nine"
  121. Case Else: ConvertDigit = ""
  122. End Select
  123.  
  124. End Function
  125.  
  126. Private Function ConvertHundreds(ByVal MyNumber)
  127. Dim Result As String
  128.  
  129. ' Exit if there is nothing to convert.
  130. If Val(MyNumber) = 0 Then Exit Function
  131.  
  132. ' Append leading zeros to number.
  133. MyNumber = Right("000" & MyNumber, 3)
  134.  
  135. ' Do we have a hundreds place digit to convert?
  136. If Left(MyNumber, 1) <> "0" Then
  137. Result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
  138. End If
  139.  
  140. ' Do we have a tens place digit to convert?
  141. If Mid(MyNumber, 2, 1) <> "0" Then
  142. Result = Result & ConvertTens(Mid(MyNumber, 2))
  143. Else
  144. ' If not, then convert the ones place digit.
  145. Result = Result & ConvertDigit(Mid(MyNumber, 3))
  146. End If
  147.  
  148. ConvertHundreds = Trim(Result)
  149. End Function
  150.  
  151.  
  152. Private Function ConvertTens(ByVal MyTens)
  153. Dim Result As String
  154.  
  155. ' Is value between 10 and 19?
  156. If Val(Left(MyTens, 1)) = 1 Then
  157. Select Case Val(MyTens)
  158. Case 10: Result = "Ten"
  159. Case 11: Result = "Eleven"
  160. Case 12: Result = "Twelve"
  161. Case 13: Result = "Thirteen"
  162. Case 14: Result = "Fourteen"
  163. Case 15: Result = "Fifteen"
  164. Case 16: Result = "Sixteen"
  165. Case 17: Result = "Seventeen"
  166. Case 18: Result = "Eighteen"
  167. Case 19: Result = "Nineteen"
  168. Case Else
  169. End Select
  170. Else
  171. ' .. otherwise it's between 20 and 99.
  172. Select Case Val(Left(MyTens, 1))
  173. Case 2: Result = "Twenty "
  174. Case 3: Result = "Thirty "
  175. Case 4: Result = "Forty "
  176. Case 5: Result = "Fifty "
  177. Case 6: Result = "Sixty "
  178. Case 7: Result = "Seventy "
  179. Case 8: Result = "Eighty "
  180. Case 9: Result = "Ninety "
  181. Case Else
  182. End Select
  183.  
  184. ' Convert ones place digit.
  185. Result = Result & ConvertDigit(Right(MyTens, 1))
  186. End If
  187.  
  188. ConvertTens = Result
  189. End Function

 

You can also rename the project title (Right click on the 'Project' title, Project Properties, provide a name you wish. For example 'MyWordTools')

Step 3: Create a shortcut button to run the Macro

Click on the customize quick access button in MS Word Window top bar and chose 'More Commands' option

MS Word - Customize Quick Access Bar

 

Now the Word options window will appear as like below.

Choose Macros from the 'Choose commands from' combo box.

Double click on the our custom module Name, now the icon will be added to the right side. Click 'Ok' to save and close.

MS Word Options - Assigning Macros to button

Now save the document as 'Macro Enabled Word document' (*.docm)

Now select the number in your document, then click on the button in the quick access bar, the number will be converted into words.

Number to Words

 

 

Category: