prkhan56 was telling us:
prkhan56 nous racontait que :
Sorry... I missed it.. I am posting it here
Try this slightly modified version.
Just replace the constant values by what you need in the lines
Const CurSym As String = "$"
Const MainCurName As String = "Dollar"
Const MinorCurSym As String = "Cent"
For the changes in text order and using () or not, you have to modify the
macro starting at:
'Deletes extra space in "millions"
vClearSpace = ClearExtraSpace()
'Type parenthesis and actual number into doc
etc.
'_______________________________________
Sub ConvertDigitToText()
Dim vOrigNum As String
Dim vOrigNumPercent As String
Dim vDollar As Integer
Dim vPercent As Integer
Dim vDecimal As Integer
Dim vStrLeft As String
Dim vStrLeftLen As Integer
Dim vStrRight As String
Dim vStrRightLen As Integer
Dim vStrChar As String
Dim vStrHoldStr As String
Dim vStrHoldStrLen As Integer
Dim vStrLeftMil As String
Dim vStrLeftBil As String
Dim vStrLeftThou As String
Dim vSrrLeftHun As String
Dim vClearSpace As String
Const CurSym As String = "$"
Const MainCurName As String = "Dollar"
Const MinorCurSym As String = "Cent"
'Assigns selection to variable
vOrigNum = Trim(Selection.Text)
'Sets length of selected number to Variable
vOrigNumLen = Len(vOrigNum)
'Checks to see if number is dollar figure
vDollar = InStr(1, vOrigNum, CurSym)
'Checks to see if number is a percent
vPercent = InStr(1, vOrigNum, "%")
'Checks to see if number is negative
vMinus = InStr(1, vOrigNum, "-")
If vMinus <> 0 Then
'If a dollar amount, then Caps; otherwise, lowercase
If vDollar <> 0 Then
'Types Minus if no. is negative
Selection.TypeText Text:="Minus "
Else
'Types minus if no. is negative
Selection.TypeText Text:="minus "
End If
End If
'Strips all but numbers from variable
For i = 1 To vOrigNumLen
vStrChar = Mid$(vOrigNum, i, 1)
Select Case vStrChar
Case ",", CurSym, "%", "-", "I", "R", "S"
Case Else
'Stripped number assigned to new variable
vStrHoldStr = vStrHoldStr & vStrChar
End Select
Next i
'Checks length of stripped number
vStrHoldStrLen = Len(vStrHoldStr)
'Checks to see if number includes decimal
vDecimal = InStr(1, vStrHoldStr, ".")
'If number includes decimal,
'assigns zeros if needed to the left or Right
If vDecimal <> 0 Then
vStrLeft = Mid(vStrHoldStr, 1, vDecimal - 1)
If vStrLeft = "" Then
'Adds left zero for ".87" type number
vStrLeft = "0"
End If
If vStrHoldStrLen - vDecimal = "0" Then
'Adds right zero for "87." type number
vStrRight = "0"
'Adds two zeros for "$87." type number
If vDollar <> 0 Then vStrRight = "00"
Else
'Assigns actual numbers to vStrRight if they exist
vStrRight = Mid(vStrHoldStr, vDecimal + 1, _
vStrHoldStrLen - vDecimal)
End If
End If
'If there is no decimal, assigns number to vStrLeft
If vDecimal = 0 Then
vStrLeft = vStrHoldStr
'and adds 0 or 00, as appropriate, to vStrRight
vStrRight = "0"
If vDollar <> 0 Then vStrRight = "00"
End If
'Assigns length of vStrLeft to vStrLeftLen
vStrLeftLen = Len(vStrLeft)
'If > billion, exit
If vStrLeftLen > 12 Then GoTo GreaterThanBillion
'If billions, strip billions string and
'insert into doc using Field
If vStrLeftLen > 9 Then
'Start at position 1, move right Length - 9 positions
vStrLeftBil = Mid(vStrLeft, 1, vStrLeftLen - 9)
'Assign leftover string to vStrLeft, start
'at Length-8, move 9 positions
vStrLeft = Mid(vStrLeft, vStrLeftLen - 8, 9)
vStrLeftLen = Len(vStrLeft)
'If a dollar amount, then Caps; otherwise, lowercase
If vDollar <> 0 Then
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
_
Text:="= " & vStrLeftBil & " \* CardText \* Caps", _
PreserveFormatting:=True
Selection.TypeText Text:=" Billion "
Else
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & vStrLeftBil & " \* CardText", _
PreserveFormatting:=True
Selection.TypeText Text:=" billion "
End If
Else
'If no billions, check millions
GoTo CheckMillions
End If
CheckMillions:
'If millions, strip millions string and insert into doc using Field
If vStrLeftLen > 6 Then
'Start at position 1, move right Length - 6 positions
vStrLeftMil = Mid(vStrLeft, 1, vStrLeftLen - 6)
'Assign leftover string to vStrLeft,
'start Length-5, move 6 positions
vStrLeft = Mid(vStrLeft, vStrLeftLen - 5, 6)
vStrLeftLen = Len(vStrLeft)
'If there is no millions, go to thousands
If vStrLeftMil = "000" Then
GoTo DoThousands
End If
'If a dollar amount, then Caps; otherwise, lowercase
If vDollar <> 0 Then
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & vStrLeftMil & " \* CardText \* Caps ", _
PreserveFormatting:=True
Selection.TypeText Text:=" Million "
Else
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & vStrLeftMil & " \* CardText ", _
PreserveFormatting:=True
Selection.TypeText Text:=" million "
End If
Else
'If no millions, do hundred thousands
GoTo DoThousands
End If
DoThousands:
'If decimal, but not dollar, insert thousands, but skip if 0
If vDecimal <> 0 And vDollar = 0 And vStrLeft <> "000000" Then
'Removed \* Caps to use lowercase
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & vStrLeft & " \* CardText", _
PreserveFormatting:=True
End If
'If decimal, but not dollar, insert left/right Fields using "point"
If vDecimal <> 0 And vDollar = 0 Then
'Deletes extra space in "millions"
vClearSpace = ClearExtraSpace()
Selection.TypeText " point "
vStrRightLen = Len(vStrRight)
'Individually insert each right side Number
For i = 1 To vStrRightLen
'Removed \* Caps to use lowercase
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & Mid(vStrRight, i, 1) & " \* CardText", _
PreserveFormatting:=True
Selection.TypeText Text:=" "
Next i
Selection.TypeBackspace
End If
'If not decimal, and not dollar, just insert Field for number words
If vDecimal = 0 And vDollar = 0 And vStrLeft <> "000000" Then
'Removed \* Caps to use lowercase
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & vStrLeft & " \* CardText", _
PreserveFormatting:=True
End If
'If percent, but not dollar, insert word "Percent"
If vPercent <> 0 And vDollar = 0 Then
'Deletes extra space in "millions"
vClearSpace = ClearExtraSpace()
Selection.TypeText Text:=" percent"
End If
'If dollar, insert Fields for left/right numbers
'w/decimal point and "Dollars"
If vDollar <> 0 Then
'Ensures decimal has only two digits
vStrRightLen = Len(vStrRight)
If vStrRightLen > 2 Then
'Strips excess digits
vStrRight = Mid(vStrRight, 1, 2)
End If
'If left of decimal = 0, and right is no 00, insert "No Dollars ""
If vStrLeft = "0" Then
Selection.TypeText Text:="No " & MainCurName & "s"
Else
'If left of decimal = 1, insert "One Dollar"
If vStrLeft = "1" Then
Selection.TypeText Text:="One " & MainCurName
'Else insert the "dollars" using "CardText"
Else
If vStrLeft <> "000000" Then
Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:="= " & vStrLeft & " \* CardText \* Caps", _
PreserveFormatting:=True
Selection.TypeText Text:=" "
End If
'Insert words after numbers
Selection.TypeText Text:=MainCurName & "s"
End If
End If
'If right of the decimal is not 00, Insert Cents
If vStrRight <> "00" Then
'Insert "and" before decimal numbers
Selection.TypeText Text:=" and "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="= " & vStrRight & " \* CardText \* Caps", _
PreserveFormatting:=True
If vStrRight = "01" Then
'Insert "Cent" for "One Cent"
Selection.TypeText Text:=" " & MinorCurSym
Else
'Insert "Cents" for other "cents"
Selection.TypeText Text:=" " & MinorCurSym & "s"
End If
Else
'Insert "No Cents " if 0 cents"
Selection.TypeText Text:=" and No " & MinorCurSym & "s"
End If
End If
'Deletes extra space in "millions"
vClearSpace = ClearExtraSpace()
'Type parenthesis and actual number into doc
Selection.TypeText Text:=" ("
'Add minus symbol
If vMinus <> 0 Then Selection.TypeText Text:="-"
'Add dollar symbol
If vDollar <> 0 Then Selection.TypeText Text:=CurSym
'Type billions and millions followed by comma
If vStrLeftBil <> "" Then Selection.TypeText Text:=vStrLeftBil & ","
If vStrLeftMil <> "" Then Selection.TypeText Text:=vStrLeftMil & ","
'Insert hundred thousands with comma
If vStrLeftLen > 3 Then
vStrLeftThou = Mid(vStrLeft, 1, vStrLeftLen - 3)
vStrLeft = Mid(vStrLeft, vStrLeftLen - 2, 3)
Selection.TypeText Text:=vStrLeftThou & ","
End If
'Insert remaining hundreds
Selection.TypeText Text:=vStrLeft
'Add decimal point if vDecimal or vDollar is true
If vDecimal <> 0 Or vDollar <> 0 Then Selection.TypeText Text:="."
'Insert right side string
Selection.TypeText Text:=vStrRight
'Remove trailing 0 if vDecimal is 0 (assigned in first routine above
'to assign vStrLeft & vStrRight
If vDecimal = 0 And vStrRight = "0" Then Selection.TypeBackspace
'Add percent symbol
If vPercent <> 0 Then Selection.TypeText Text:="%"
'Insert closing parenthesis
Selection.TypeText Text:=")"
'Jumps over GreatThanBillion error message
Exit Sub
'GreaterThanBillion error message
GreaterThanBillion:
MsgBox "Number is Greater than 999,999,999,999.99!" & vbCr & _
"Macro will now terminate.", vbExclamation, "Invalid Number"
End Sub
'_______________________________________
'_______________________________________
Function ClearExtraSpace()
'Deletes extra space when "million " used in some cases
If Selection.Characters.First.Previous = " " Then
Selection.TypeBackspace
End If
End Function
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org