MS Office Forum / Word / Programming / September 2007
How to deduce date in words in Word?
|
|
Thread rating:  |
avkokin - 02 Sep 2007 16:50 GMT How to deduce date in words in Word? For example: July 10, 2007 should be as "Tenth of July two thousand seventh year"
Dav - 02 Sep 2007 21:24 GMT Hi Avtokin
Try this in your document using Ctrl+F9
{DATE \@ "dd" \* CARDTEXT} {DATE \@ "MMMM"} {DATE \@ "yyyy" \* CARDTEXT}
Should be ok like this. Let us know
Greetings from France Dav
> How to deduce date in words in Word? For example: July 10, 2007 should > be as "Tenth of July two thousand seventh year" Dav - 02 Sep 2007 21:34 GMT I have a bit better
Replace the first switch with \* ORDTEXT for the day instead of \* CARDTEXT It should bring you exactly what you want
Greetings from France
Dav
> Hi Avtokin > [quoted text clipped - 9 lines] >> How to deduce date in words in Word? For example: July 10, 2007 should >> be as "Tenth of July two thousand seventh year" Greg Maxey - 03 Sep 2007 00:22 GMT > 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
avkokin - 03 Sep 2007 07:39 GMT > > How to deduce date in words in Word? For example: July 10, 2007 should > > be as "Tenth of July two thousand seventh year" [quoted text clipped - 40 lines] > End If > End Sub Thank you very much. I should explain the requirements. It is necessary to insert date into the document. But date should consist of words completely. That is it is necessary to convert date at line (day, month and year). Your code almost satisfies to my inquiries and works well. But year should not be set rigidly. Thanks for the help.
Graham Mayor - 03 Sep 2007 12:14 GMT I think we have a small language problem here :) Do you want to insert the date, or convert a date that is already inserted? If the former, a slight modification to Greg's code will do the job:
Sub ScratchMacro() Dim pDate As Date Dim pStr As String Dim pBuildStr As String pStr = Format(Date, "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 = "2005": pBuildStr = pBuildStr + "two thousand fifth" Case Is = "2008": pBuildStr = pBuildStr + "two thousand sixth" Case Is = "2007": pBuildStr = pBuildStr + "two thousand seventh" Case Is = "2008": pBuildStr = pBuildStr + "two thousand eighth" Case Is = "2009": pBuildStr = pBuildStr + "two thousand ninth" Case Else MsgBox ("Date Out Of Range") Exit Sub End Select Selection.TypeText pBuildStr & " year" End Sub
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>>> How to deduce date in words in Word? For example: July 10, 2007 >>> should be as "Tenth of July two thousand seventh year" [quoted text clipped - 48 lines] > inquiries and works well. But year should not be set rigidly. Thanks > for the help. Dav - 03 Sep 2007 17:21 GMT Hi there
Why making things so long and complicated when it could so simple and easy using things which already exist?
For exemple :
Sub IncludeDate() ' Selection.Fields.Add Range:=Selection.Range, Text:="DATE \@ ""dd"" \* ORDTEXT", PreserveFormatting:=True Selection.TypeText Text:=" of " Selection.Fields.Add Range:=Selection.Range, Text:="DATE \@ ""MMMM""", PreserveFormatting:=True Selection.TypeText Text:=" " Selection.Fields.Add Range:=Selection.Range, Text:="DATE \@ ""yyyy"" \* ORDTEXT", PreserveFormatting:=True Selection.TypeText Text:=" year" End Sub
And it works very well.
Maybe i am missing the point...
Greetings from France
Dav
>I think we have a small language problem here :) > Do you want to insert the date, or convert a date that is already [quoted text clipped - 105 lines] >> inquiries and works well. But year should not be set rigidly. Thanks >> for the help. Greg Maxey - 03 Sep 2007 22:33 GMT Dav,
I wasn't trying to make things long or complicated. I simply enjoy writing code. Otherwise I wouldn't be here ;-)
I think your suggestion is fine if the user only wants to insert today's date.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Hi there > [quoted text clipped - 133 lines] >>> inquiries and works well. But year should not be set rigidly. Thanks >>> for the help. Greg Maxey - 04 Sep 2007 00:30 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Hi there > [quoted text clipped - 141 lines] >>> inquiries and works well. But year should not be set rigidly. Thanks >>> for the help. avkokin - 04 Sep 2007 09:37 GMT > Here is something even longer and more complicated that should handle this > and the last millenium: [quoted text clipped - 285 lines] > > read more ? Thank you, Greg. Your second code is that what I need. Hearty thanks all who has answered!
Graham Mayor - 04 Sep 2007 08:45 GMT Another problem is that you are inserting date fields which will update to show the system date of the computer, which is rarely what is required. You could modify it to include createdate fields, which would be a bit better
Sub IncludeDate() With Selection .Fields.Add Range:=Selection.Range, Text:="CREATEDATE \@ ""dd"" \*ORDTEXT \*CAPS" .TypeText Text:=" of " .Fields.Add Range:=Selection.Range, Text:="CREATEDATE \@ ""MMMM""" .TypeText Text:=" " .Fields.Add Range:=Selection.Range, Text:="CREATEDATE \@ ""yyyy"" \*ORDTEXT " .TypeText Text:=" year" End With End Sub
but if you want more flexibility the simpleset solutions are not always the best solutions. If using fields, macropod's solution is the neatest, but even that could do with a minor modification thus:
{ QUOTE{ ASK MyDate "Please Input the date" \d { Date}} { MyDate \@ "'{ MyDate \@ d \* OrdText } of' MMMM, "\*FirstCap } { =INT({ MyDate \@ yyyy }/100)*100 \* CardText } { IF{ =MOD({ MyDate \@ yyyy },100) }= 0 "th" " and { =MOD({ MyDate \@ yyyy },100) \* OrdText }" }" year." }
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi there > [quoted text clipped - 141 lines] >>> inquiries and works well. But year should not be set rigidly. Thanks >>> for the help. macropod - 04 Sep 2007 08:55 GMT Of course one could use a macro to input simple date fields, then lock them or convert them to plain text. Might be a whole lot simpler than the other coding solutions in this thread ...
Cheers
 Signature macropod [MVP - Microsoft Word] -------------------------
> Another problem is that you are inserting date fields which will update to show the system date of the computer, which is rarely > what is required. You could modify it to include createdate fields, which would be a bit better [quoted text clipped - 163 lines] >>>> inquiries and works well. But year should not be set rigidly. Thanks >>>> for the help. macropod - 03 Sep 2007 12:55 GMT How about a field code solution: {QUOTE{ASK MyDate "Please Input the date"} {MyDate \@ "dddd 'the {MyDate \@ d \* OrdText} of' MMMM, "} {=INT({MyDate \@ yyyy}/100)*100 \* CardText} {IF{=MOD({MyDate \@ yyyy},100)}= 0 "th" " and {=MOD({MyDate \@ yyyy},100) \* OrdText}"}" year."} to return text like "Saturday the first of January, two thousandth year." and "Monday the first of January, two thousand and seventh year." Alternatively: {QUOTE{ASK MyDate "Please Input the date"} {MyDate \@ "dddd 'the { MyDate \@ d \* OrdText} of' MMMM, "} {MyDate \@ yyyy \* OrdText}" year."} to return text like "Saturday the first of January, two thousandth year." and "Monday the first of January, two thousand seventh year." The difference between these two is the inclusion of code to insert 'and' in the year string for the first example for years other than centuries.
Cheers
 Signature macropod [MVP - Microsoft Word] -------------------------
> How to deduce date in words in Word? For example: July 10, 2007 should > be as "Tenth of July two thousand seventh year"
|
|
|