A
avkokin
How to deduce date in words in Word? For example: July 10, 2007 should
be as "Tenth of July two thousand seventh year"
be as "Tenth of July two thousand seventh year"
How to deduce date in words in Word? For example: July 10, 2007 should
be as "Tenth of July two thousand seventh year"
I am not certain of your requirements and this code can most likely be
refined further, but if you range of dates is reasonable (e.g., this
year, the last few years, or the next few years) I suppose something
like this may do.
Select the date in the document and run this code:
Sub ScratchMacro()
Dim pDate As Date
Dim pStr As String
Dim pBuildStr As String
If IsDate(Selection.Range) Then
pDate = Selection.Range.Text
pStr = Format(pDate, "dd MMMM yyyy")
Select Case Left(pStr, 2)
Case Is = "01": pBuildStr = "First day of "
Case Is = "02": pBuildStr = "Second day of "
Case Is = "03": pBuildStr = "Third day of "
Case Is = "04": pBuildStr = "Fourth day of "
Case Is = "05": pBuildStr = "Fifth day of "
Case Is = "06": pBuildStr = "Sixth day of "
Case Is = "07": pBuildStr = "Seventh day of "
Case Is = "08": pBuildStr = "Eigth day of "
'And so on
End Select
pBuildStr = pBuildStr + Mid(pStr, 4, Len(pStr) - 7)
Select Case Right(pDate, 4)
Case Is = "2005": pBuildStr = pBuildStr + "two thousand fifth
year"
Case Is = "2008": pBuildStr = pBuildStr + "two thousand sixth
year"
Case Is = "2007": pBuildStr = pBuildStr + "two thousand seventh
year"
Case Is = "2008": pBuildStr = pBuildStr + "two thousand eight
year"
Case Is = "2009": pBuildStr = pBuildStr + "two thousand ninth
year"
End Select
Selection.Range.Text = pBuildStr
End If
End Sub
Here is something even longer and more complicated that should handle this
and the last millenium:
Sub ScratchMacro()
Dim pDate As Date
Dim pStr As String
Dim pBuildStr As String
Dim bRefine As Boolean
On Error GoTo Err_Handler
pDate = CDate(InputBox("Enter the date", "Date"))
pStr = Format(pDate, "dd MMMM yyyy")
Select Case Left(pStr, 2)
Case Is = "01": pBuildStr = "First of "
Case Is = "02": pBuildStr = "Second of "
Case Is = "03": pBuildStr = "Third of "
Case Is = "04": pBuildStr = "Fourth of "
Case Is = "05": pBuildStr = "Fifth of "
Case Is = "06": pBuildStr = "Sixth of "
Case Is = "07": pBuildStr = "Seventh of "
Case Is = "08": pBuildStr = "Eighth of "
Case Is = "09": pBuildStr = "Ninth of "
Case Is = "10": pBuildStr = "Tenth of "
Case Is = "11": pBuildStr = "Eleventh of "
Case Is = "12": pBuildStr = "Twelfth of "
Case Is = "13": pBuildStr = "Thirteenth of "
Case Is = "14": pBuildStr = "Fourteenth of "
Case Is = "15": pBuildStr = "Fifteenth of "
Case Is = "16": pBuildStr = "Sixteenth of "
Case Is = "17": pBuildStr = "Seventeenth of "
Case Is = "18": pBuildStr = "Eighteenth of "
Case Is = "19": pBuildStr = "Nineteenth of "
Case Is = "20": pBuildStr = "Twentieth of "
Case Is = "21": pBuildStr = "Twenty first of "
Case Is = "22": pBuildStr = "Twenty second of "
Case Is = "23": pBuildStr = "Twenty third of "
Case Is = "24": pBuildStr = "Twenty fourth of "
Case Is = "25": pBuildStr = "Twenty fifth of "
Case Is = "26": pBuildStr = "Twenty sixth of "
Case Is = "27": pBuildStr = "Twenty seventh of "
Case Is = "28": pBuildStr = "Twenty eighth of "
Case Is = "29": pBuildStr = "Twenty ninth of "
Case Is = "30": pBuildStr = "Thirtieth of "
Case Is = "31": pBuildStr = "Thirty first of "
End Select
pBuildStr = pBuildStr + Mid(pStr, 4, Len(pStr) - 7)
Select Case Right(pStr, 4)
Case Is = "1900": pBuildStr = pBuildStr + "nineteen hundredth"
Case Is = "2000": pBuildStr = pBuildStr + "two thousandth"
Case Else
Select Case Mid(pStr, Len(pStr) - 3, 1)
Case Is = "1": pBuildStr = pBuildStr + "one thousand"
Case Is = "2": pBuildStr = pBuildStr + "two thousand"
Case Else
MsgBox ("Date Out Of Range")
Exit Sub
End Select
Select Case Mid(pStr, Len(pStr) - 2, 1)
Case Is = "0": pBuildStr = pBuildStr + " "
Case Is = "1"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " one hundredth"
Else
pBuildStr = pBuildStr + " one hundred"
End If
Case Is = "2"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " two hundredth"
Else
pBuildStr = pBuildStr + " two hundred"
End If
Case Is = "3"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " three hundredth"
Else
pBuildStr = pBuildStr + " three hundred"
End If
Case Is = "4"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " four hundredth"
Else
pBuildStr = pBuildStr + " four hundred"
End If
Case Is = "5"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " five hundredth"
Else
pBuildStr = pBuildStr + " five hundred"
End If
Case Is = "6"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " six hundredth"
Else
pBuildStr = pBuildStr + " six hundred"
End If
Case Is = "7"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " seven hundredth"
Else
pBuildStr = pBuildStr + " seven hundred"
End If
Case Is = "8"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " eight hundredth"
Else
pBuildStr = pBuildStr + " eight hundred"
End If
Case Is = "9"
If Right(pStr, 2) = "00" Then
pBuildStr = pBuildStr + " nine hundredth"
Else
pBuildStr = pBuildStr + " nine hundred"
End If
End Select
Select Case Mid(pStr, Len(pStr) - 1, 1)
Case Is = "0"
bRefine = True
Case Is = "1"
Select Case Right(pStr, 1)
Case Is = "0": pBuildStr = pBuildStr + " tenth"
Case Is = "1": pBuildStr = pBuildStr + " eleventh"
Case Is = "2": pBuildStr = pBuildStr + " twelveth"
Case Is = "3": pBuildStr = pBuildStr + " thirteenth"
Case Is = "4": pBuildStr = pBuildStr + " fourteeth"
Case Is = "5": pBuildStr = pBuildStr + " fifteenth"
Case Is = "6": pBuildStr = pBuildStr + " sixteeth"
Case Is = "7": pBuildStr = pBuildStr + " seventeeth"
Case Is = "8": pBuildStr = pBuildStr + " eighteenth"
Case Is = "9": pBuildStr = pBuildStr + " nineteenth"
End Select
Case Is = "2"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " twentieth"
Else
pBuildStr = pBuildStr + " twenty"
bRefine = True
End If
Case Is = "3"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " thritieth"
Else
pBuildStr = pBuildStr + " thirty"
bRefine = True
End If
Case Is = "4"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " fortieth"
Else
pBuildStr = pBuildStr + " forty"
bRefine = True
End If
Case Is = "5"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " fiftieth"
Else
pBuildStr = pBuildStr + " fifty"
bRefine = True
End If
Case Is = "6"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " sixtieth"
Else
pBuildStr = pBuildStr + " sixty"
bRefine = True
End If
Case Is = "7"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " seventieth"
Else
pBuildStr = pBuildStr + " seventy"
bRefine = True
End If
Case Is = "8"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " eightieth"
Else
pBuildStr = pBuildStr + " eighty"
bRefine = True
End If
Case Is = "9"
If Right(pStr, 1) = "0" Then
pBuildStr = pBuildStr + " ninetieth"
Else
pBuildStr = pBuildStr + " ninety"
bRefine = True
End If
Case Else
MsgBox ("Date Out Of Range")
Exit Sub
End Select
End Select
If bRefine Then
Select Case Right(pStr, 1)
Case Is = "1": pBuildStr = pBuildStr + " first"
Case Is = "2": pBuildStr = pBuildStr + " second"
Case Is = "3": pBuildStr = pBuildStr + " third"
Case Is = "4": pBuildStr = pBuildStr + " fourth"
Case Is = "5": pBuildStr = pBuildStr + " fifth"
Case Is = "6": pBuildStr = pBuildStr + " sixth"
Case Is = "7": pBuildStr = pBuildStr + " seventh"
Case Is = "8": pBuildStr = pBuildStr + " eighth"
Case Is = "9": pBuildStr = pBuildStr + " ninth"
End Select
End If
Selection.TypeText pBuildStr & " year"
Exit Sub
Err_Handler:
If Err.Number = 13 Then
MsgBox "You must enter a valid date format e.g., 11 SEP 2000"
Resume
End If
End Sub
--
Greg Maxey/Word MVP
See:http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
...
read more »
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.