View Full Version : [SOLVED:] Convert date into words (with a twist)
DavG63
04-25-2017, 05:36 AM
Hi all
I have a document I'm working on just now where the user inserts a date in UK format, I've put an examples below.
I've used cardtext in the past to covert numeric strings into words and I know how to change the date from short to long format easily enough just by tweaking the DocVariable.
Date: 01/01/2017
Intended output: First day of January Two Thousand and Seven.
Can anyone suggest how I would go about doing that?
Thanks in advance for any suggestions.
Dav
gmayor
04-25-2017, 06:39 AM
Paul is the expert on fields, but the following should do the job and will insert the correct year :)
{ SET A { DATE \@ "d" } }{ IF{ A } = 1 { SET D "First"} }{ IF { A } = 2 { SET D "Second"} }{ IF{ A } = 3 { SET D "Third"} }{ IF{ A } = 4 { SET D "Fourth"} }{ IF{ A } = 5 { SET D "Fifth"} }{ IF{ A } = 6 { SET D "Sixth"} }{ IF{ A } = 7 { SET D "Seventh"} }{ IF{ A } = 8 { SET D "Eighth"} }{ IF{ A } = 9 { SET D "Ninth"} }{ IF{ A } = 10 { SET D "Tenth"} }{ IF{ A } = 11 { SET D "Eleventh"} }{ IF{ A } = 12 { SET D "Twelfth"} }{ IF{ A } = 13 { SET D "Thirteenth"} }{ IF{ A } = 14 { SET D "Fourteenth"} }{ IF{ A } = 15 { SET D "Fifteenth"} }{ IF{ A } = 16 { SET D "Sixteenth"} }{ IF{ A } = 17 { SET D "Seventeenth"} }{ IF{ A } = 18 { SET D "Eighteenth"} }{ IF{ A } = 19 { SET D "Nineteenth"} }{ IF{ A } = 20 { SET D "Twentieth"} }{ IF{ A } = 21 { SET D "Twenty First"} }{ IF{ A } = 22 { SET D "Twenty Second"} }{ IF{ A } = 23 { SET D "Twenty Third"} }{ IF{ A } = 24 { SET D "Twenty Fourth"} }{ IF{ A } = 25 { SET D "Twenty Fifth"} }{ IF{ A } = 26 { SET D "Twenty Sixth"} }{ IF{ A } = 27 { SET D "Twenty Seventh"} }{ IF{ A } = 28 { SET D "Twenty Eighth"} }{ IF{ A } = 29 { SET D "Twenty Ninth"} }{ IF{ A } = 30 { SET D "Thirtieth"} }{ IF{ A } = 31 { SET D "Thirty First"} }{ D } day of { DATE \@ "MMMM" } { DATE \@ "yyyy" \*Cardtext \*Caps }
You will almost certainly need http://www.gmayor.com/export_field.htm to put that back to working fields without errors.
DavG63
04-25-2017, 07:14 AM
Um... ok... wow! :whistle::whistle:
That actually hurts my brain just looking at it.
My date is fed through a Userform to a DocVariable called "Date". Am I right in thinking that I should exchange "Date" in your example for "DocVariable Date"?
Thanks
Dav
EDIT: Never mind, I just tried it and it worked. Mind blown.
Graham - thanks so much.
macropod
04-25-2017, 01:40 PM
I'd prefer to keep it simple:
{DocVariable DATE \@ DD \* OrdText} day of {DocVariable DATE \@ MMMM}, two thousand and {DocVariable DATE \@ YY \* CardText}
Although one could use:
{DocVariable DATE \@ DD \* OrdText} day of {DocVariable DATE \@ MMMM}, {DocVariable DATE \@ YYYY \* CardText}
that omits the 'and' that civilised countries include in such expressions.
If you need to deal with previous centuries as well, you could use:
{DocVariable DATE \@ DD \* OrdText} day of {DocVariable DATE \@ MMMM}, {=INT({DocVariable DATE \@ YYYY}/1000)*1000 \* CardText} and {DocVariable DATE \@ YY \* CardText}
You can also add the \* Caps switch to the end of any of the above fields for which you want to capitalize the first letter of each word.
gmaxey
04-25-2017, 02:45 PM
Here is another suggestion:
Create the following field code and save it in normal template AutoText buildingblock gallery usingthe name "DocVariableDate"
{DocVariable Date \@ DD \*OrdText \*FirstCap } day of { DocVariable Date \@ MMMM}, { =({ DocVariable Date \@ YYYY} - { DocVariable Date \@ YY }) \*CardText\*Caps } { IF { DocVariable Date \@ YY } > 0 "and { DocVariable Date \@YY \*CardText \*Caps }""" }
At a Date content control titled "Spelled Out Date" and define the UK date format property.
Add the following code to the ThisDocument Module
Option Explicit
Private Sub Document_ContentControlOnEnter(ByVal CC As ContentControl)
Select Case CC.Title
Case "Spelled Out Date"
CC.Range.Text = vbNullString
CC.Type = wdContentControlDate
End Select
lbl_Exit:
Exit Sub
End Sub
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Select Case CC.Title
Case "Spelled Out Date"
If IsDate(CC.Range.Text) Then
CC.Type = wdContentControlRichText
ActiveDocument.Variables("Date").Value = CC.Range.Text
NormalTemplate.BuildingBlockEntries("DocVariableDate").Insert Where:=CC.Range, RichText:=True
End If
End Select
lbl_Exit:
Exit Sub
End Sub
gmayor
04-25-2017, 08:20 PM
I'd forgotten about the OrdText switch. I must be getting too old for this.:(
macropod
04-25-2017, 09:54 PM
We 'oldies' all have our senior moments...
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.