Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Get date from string (WordXPSP3/WinXP)

    How do I get a string (in a variable) like Apr04 to be understood by Word as a date?! I need to take a string entered in this format ( which is needed as is) and get 'April 2004' from it as well, and I could have sworn it was something like the following:

    Dim ChatMonth as string, ChatDate as date
    ChatMonth = Ask("Chat Publication Month (Mmmyy)")
    ChatDate = Format(ChatMonth, "mmmyy")
    ChatDate = Format(ChatDate, "mmmm yyyy")

    But this gets a type mismatch error! <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

    Any help would be gratefully received!
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Get date from string (WordXPSP3/WinXP)

    I would tell the users to enter the date in a valid format, but that is probably not what you wanted to hear. So try this code:

    Dim ChatMonth as String, ChatDate as Date
    ChatMonth = Ask("Chat Publication Month (Mmmyy)")
    ChatDate = DateValue("1/" & Left(ChatMonth, 3) & "/" & Right(ChatMonth, 2))

    I assume that Ask is a custom function you use (it is a field in Word, but not a VBA function)

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Get date from string (WordXPSP3/WinXP)

    Thanks, Hans, and yes, Ask is a custom field, but one that is built into our 'library' of standard code so that I forget it's not universal!

    I shall try the suggestion as soon as I get back to the machine it's on!

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> as usual!
    Beryl M


  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Get date from string (WordXPSP3/WinXP)

    You also can use the CDate convertion function, with appropriate error trapping, to get yourself a date variable to convert to a formatted string. The function behaves differently in different regions, so the results obviously are not as predictable as processing it yourself...

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Get date from string (WordXPSP3/WinXP)

    Hi Beryl,

    If you're using an ASK field to solicit a date that simply needs to be output in the document in the required format, you could use a field coded as:
    {QUOTE{ASK ChatDate }{ChatDate @ "MMMM yyyy"}}
    Word will recognise the string 'Apr04' in a field as a date, which you can reformat with the appropriate field switches.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Get date from string (WordXPSP3/WinXP)

    Hmm - does that work bearing in mind that ASK in this case is a custom function, very similar to inputbox?
    Beryl M


  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Get date from string (WordXPSP3/WinXP)

    Hi Beryl,

    I can't say for a custom function, but a REF field can do this for any bookmarked date string. The example I posted uses an ASK field (which works somewhat like an inputbox) to set a bookmark and a REF field to format the result. They were both wrapped in a QUOTE field to force the REF field to update immediately the ASK field updates the bookmark.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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