Convert Number to Words Currency in MS Word

Convert Number to Words Currency in MS Word


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,

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 Module
----------------


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


-------------------------------

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
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.


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.




For Any Help you can contact me on my Contact me on My Email gauravj75@gmail.com or Comment here.

Comments

Popular posts from this blog

GSTIN CheckSum Digit Logic

Spell-Number in Rupees Excel VBA Formula