Consulting

Results 1 to 7 of 7

Thread: Convert date into words (with a twist)

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location

    Convert date into words (with a twist)

    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

  2. #2
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    54
    Location
    Um... ok... wow!

    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.
    Last edited by DavG63; 04-25-2017 at 07:27 AM.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    

    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    I'd forgotten about the OrdText switch. I must be getting too old for this.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    We 'oldies' all have our senior moments...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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