Re: How to Create word Macro that converts currency in numbers into words? Open MSword
Click file>close (no more open documents)
Click tools>macros>Visual basic editor
On the left hand side tree-view, expand 'normal'>'Microsoft word objects' to see 'ThisDocument'
Double click on 'ThisDocument'
A white (probably blank) window will appear on the
right hand side, where you should paste the following code
Close all the windows. Installation is complete!
Now when you are typing a document,
if want to use the converter,
Select the number which you want to convert.
Press Alt+F8.
Press Enter. (check for 'RSconvert' in 'Macro name' text field)
This will replace the selected number with its
word equivalent
Dim arrSingleDigit
Dim arrTeen
Dim arrTwoDigits
Sub RSconvert()
On Error Resume Next
arrSingleDigit = Array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
arrTeen = Array("eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty")
arrTwoDigits = Array("10", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety ", "Hundred")
valdig = Trim("" + Selection.Text)
If valdig = "" Or IsNumeric(valdig) = False Then Exit Sub
valInDigits = valdig
paisa = Int(valInDigits * 100 - Int(valInDigits) * 100)
valInDigits = Int(valInDigits)
If valInDigits > 0 Then
If valInDigits > 999999999 Then
MsgBox "Beyond Limit"
Else
msgtmp = convert(valInDigits) + " Rupees"
If paisa <> 0 Then msgtmp = msgtmp + " and " + convert(paisa) + " Paisa"
msgtmp = UCase(Left(msgtmp, 1)) + Right(msgtmp, Len(msgtmp) - 1)
Selection.Text = msgtmp
Exit Sub
End If
End If
End Sub
Private Function convert(valInDigits)
Dim MSB
Dim LSB
If valInDigits > 0 And valInDigits < 10 Then
converted = arrSingleDigit(valInDigits - 1)
ElseIf valInDigits = 10 Then
converted = "ten"
ElseIf ((valInDigits > 10) And (valInDigits < 20)) Then
converted = arrTeen(valInDigits Mod 10 - 1)
ElseIf ((valInDigits >= 20) And (valInDigits <= 99)) Then
MSB = valInDigits \ 10
converted = arrTwoDigits(MSB - 1)
LSB = valInDigits Mod 10
If LSB > 0 Then
converted = converted + convert(LSB)
End If
ElseIf ((valInDigits >= 100) And (valInDigits <= 999)) Then
MSB = valInDigits \ 100
converted = convert(MSB) + " " + "hundred"
LSB = valInDigits Mod 100
If LSB > 0 Then
converted = converted + " " + convert(LSB)
End If
ElseIf ((valInDigits >= 999) And (valInDigits <= 99999)) Then
MSB = valInDigits \ 1000
converted = convert(MSB) + " " + "thousand"
LSB = valInDigits Mod 1000
If LSB > 0 Then
converted = converted + " " + convert(LSB)
End If
ElseIf ((valInDigits >= 99999) And (valInDigits <= 9999999)) Then
MSB = valInDigits \ 100000
converted = convert(MSB) + " " + "lakh"
LSB = valInDigits Mod 100000
If LSB > 0 Then
converted = converted + " " + convert(LSB)
End If
ElseIf ((valInDigits >= 9999999) And (valInDigits <= 999999999)) Then
MSB = valInDigits \ 10000000
converted = convert(MSB) + " " + "crore"
LSB = valInDigits Mod 10000000
If LSB > 0 Then
converted = converted + " " + convert(LSB)
End If
End If
convert = converted
End Function |