Convert Currency in Number to Words in MS Word - OneSite

Breaking

EVERYTHING IS HERE

Post Top Ad

Post Top Ad

Saturday, September 7, 2019

Convert Currency in Number to Words in MS Word

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

No comments:

Post a Comment

Post Top Ad