Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Convert numbers to text

    (Yet another question!)
    Is it possible to convert numbers to text. There are two scenarios that I can think of:
    1 - I would like to convert a date such as 31/05/2001 to text so that it prints Dated this 31st day of May 2001.
    2 - Convert a number 150500 to text so that it prints one hundred and fifty thousand and five hundred dollars

    At this stage I make my users manually type the result in, so it's for future reference really.

    I've had a look at the data type conversion functions but cannot find one. Is it possible?

    I'm sure I've read something about this in one of these forums, but have tried searching for data conversion and can't seem to find anything.

    Thanks for your comments.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Convert numbers to text

    Two different cuts at the same thing (gleaned from days gone by here). I also have a version in WordBasic if you needed it.
    With your dates I would consider formatting the date first and then working on that individually.

    <pre>Sub CardText()
    Dim sDigits As String

    ' Select the full number in which the insertion point is located
    Selection.MoveLeft Unit:=wdWord, count:=1, Extend:=wdMove
    Selection.MoveRight Unit:=wdWord, count:=1, Extend:=wdExtend

    ' Store the digits in a variable
    sDigits = Selection.Text

    If Val(sDigits) <= 999999 Then
    ' Create a field containing the digits and the cardtext format flag
    Selection.Fields.Add Range:=Selection.Range, _
    Type:=wdFieldEmpty, Text:="= " + sDigits + " * CardText", _
    PreserveFormatting:=True

    ' Select the field and copy it
    Selection.MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdExtend
    Selection.Copy

    ' Now paste the text as 'unformatted', replacing the selected field
    Selection.PasteSpecial Link:=False, DataType:=wdPasteText, _
    Placement:=wdInLine, DisplayAsIcon:=False

    ' Add space after words
    Selection.TypeText Text:=" "
    Else
    MsgBox "Number too large", vbOKOnly
    End If
    End Sub

    '======================================
    Sub NumberToWords()
    Dim Number As Long
    Dim Words As String
    Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    If IsNumeric(Selection) Then
    Number = CLng(Selection)
    Select Case Number
    Case 0
    Words = "Zero"
    Case 1 To 999999
    Words = SetThousands(Number)
    Case Else
    MsgBox "Number too large!", vbExclamation, "NumberToWords Macro"
    End Select
    Else
    MsgBox "No number to left of insertion point!", _
    vbExclamation, "NumberToWords Macro"
    End If
    Selection = Words
    End Sub

    Private Function SetOnes(ByVal Number As Integer) As String
    Dim OnesArray(9) As String
    OnesArray(1) = "One"
    OnesArray(2) = "Two"
    OnesArray(3) = "Three"
    OnesArray(4) = "Four"
    OnesArray(5) = "Five"
    OnesArray(6) = "Six"
    OnesArray(7) = "Seven"
    OnesArray(8) = "Eight"
    OnesArray(9) = "Nine"
    SetOnes = OnesArray(Number)
    End Function

    Private Function SetTens(ByVal Number As Integer) As String
    Dim TensArray(9) As String
    TensArray(1) = "Ten"
    TensArray(2) = "Twenty"
    TensArray(3) = "Thirty"
    TensArray(4) = "Fourty"
    TensArray(5) = "Fifty"
    TensArray(6) = "Sixty"
    TensArray(7) = "Seventy"
    TensArray(8) = "Eighty"
    TensArray(9) = "Ninety"
    Dim TeensArray(9) As String
    TeensArray(1) = "Eleven"
    TeensArray(2) = "Twelve"
    TeensArray(3) = "Thirteen"
    TeensArray(4) = "Fourteen"
    TeensArray(5) = "Fifteen"
    TeensArray(6) = "Sixteen"
    TeensArray(7) = "Seventeen"
    TeensArray(8) = "Eighteen"
    TeensArray(9) = "Nineteen"
    Dim tmpInt1 As Integer
    Dim tmpInt2 As Integer
    Dim tmpString As String
    tmpInt1 = Int(Number / 10)
    tmpInt2 = Int(Number Mod 10)
    tmpString = TensArray(tmpInt1)
    If (tmpInt1 = 1 And tmpInt2 > 0) Then
    tmpString = TeensArray(tmpInt2)
    Else
    If (tmpInt1 > 1 And tmpInt2 > 0) Then
    tmpString = tmpString + " " + SetOnes(tmpInt2)
    End If
    End If
    SetTens = tmpString
    End Function

    Private Function SetHundreds(ByVal Number As Integer) As String
    Dim tmpInt1 As Integer
    Dim tmpInt2 As Integer
    Dim tmpString As String
    tmpInt1 = Int(Number / 100)
    tmpInt2 = Int(Number Mod 100)
    If tmpInt1 > 0 Then tmpString = SetOnes(tmpInt1) + " Hundred"
    If tmpInt2 > 0 Then
    If tmpString > "" Then tmpString = tmpString + " "
    If tmpInt2 < 10 Then tmpString = tmpString + SetOnes(tmpInt2)
    If tmpInt2 > 9 Then tmpString = tmpString + SetTens(tmpInt2)
    End If
    SetHundreds = tmpString
    End Function

    Private Function SetThousands(ByVal Number As Long) As String
    Dim tmpInt1 As Integer
    Dim tmpInt2 As Integer
    Dim tmpString As String
    tmpInt1 = Int(Number / 1000)
    tmpInt2 = Int(Number Mod 1000)
    If tmpInt1 > 0 Then tmpString = SetHundreds(tmpInt1) + " Thousand"
    If tmpInt2 > 0 Then
    If tmpString > "" Then tmpString = tmpString + " "
    tmpString = tmpString + SetHundreds(tmpInt2)
    End If
    SetThousands = tmpString
    End Function</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,048
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Convert numbers to text

    You can use field switches to format your text.

    Check out the following switches:

    * cardtext
    Converts numbers in the result to cardinal text form. By default, an initial capital letter is used. For a different case, add another format switch that specifies capitalization. For example, {= sum(A1:B2) * cardtext} gives a result such as "Seven Hundred Ninety." The field {= sum(A1:B2) * cardtext * lower} gives a result such as "seven hundred ninety."

    * dollartext
    Converts numbers in the result to cardinal text, inserts "and" at the decimal place, and displays the first two decimals (rounded) as arabic numerators over 100. For example, {=9.20 + 5.35 * dollartext} gives the result "Fourteen and 55/100."

    * ordinal
    Converts numbers in the result to arabic ordinal form. For example, {DATE @ "d" * ordinal} gives a result such as "30th."

    * ordtext
    Converts numbers in the result to ordinal text form. By default, an initial capital letter is used. For a different case, add another format switch that specifies capitalization. For example, {DATE @ "d" * ordtext} gives a result such as "Fourteenth." The field {DATE @ "d" * ordtext * upper} gives a result such as "FOURTEENTH."

    The explanations are taken from <A target="_blank" HREF=http://support.microsoft.com/support/word/usage/fields/GeneralSwitches.asp> General Switches for Fields</A> on the Microsoft site.
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,048
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Convert numbers to text

    Hi Karen,

    For more on formatting dates see <A target="_blank" HREF=http://www.addbalance.com/word/datefields1.htm>Inserting a Date - and Making it Stick!</A> (addbalance.com/word/datefields1.htm). (when my site is back up <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15>)
    Charles Kyle Kenyon
    Madison, Wisconsin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •